On Wed, 10 May 2006 at 10:04pm, Gerald Lai wrote: > On Wed, 10 May 2006, Ben K. wrote: > > > > > Hi, > > > > Is there a way to split a line automatically like awk would? > > > > Given "A quick brown fox jumped over ", > > awk '{print $3}' ... ==> brown > > or like in perl > > split(':',$line)... > > > > I'd like to do within vim something like > > :s/{some notation}/\3 > > without having to define the pattern > > :s/\(\S\+\) \(\S\+\) \(\S\+\) ... /\3 ==> brown > > > > using white space, and if needed, by defining my own separator > > The regex to search for the <n>th occurence of a <search> is > > /^\%(.\{-}\zs<search>\)\{<n>} > > Given a line like > > :A:quick:brown:fox:jumped:over > > you can search for the <n>th word with > > /^\%(.\{-}:\zs[^:]*\)\{<n>} > > For example, if you want "fox", then do > > /^\%(.\{-}:\zs[^:]*\)\{4} > > You can easily modify the regex to use whitespace as delimiters. > > HTH.
In Vim7, you can use the split function as: :s/.*/\=split(submatch(0))[2]/ The above will replace the whole currently line with the third word (0 based index). -- HTH, Hari __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com