grep return only one match per line -


example file :

foobar random text foobar random text foobar text text text 

if use grep , search word foobar, how can prevent grep return me first line 3 times, because founds 3 times foobar ? have 1 return per line, if word has been found multiple times on line

simple awk alternative:

awk '/\<foobar\>/{print nr,"foobar"}' file 

the output(for exemplary input):

1 foobar 

\< , \> mean word boundaries

nr - contains current line number



perl:

perl -ne 'print $.," ",$1,"\n" if /\b(foobar)\b/' file 

the output:

1 foobar 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -