Search code examples
perlunixawksolarisnawk

How to get the lines from a file where a particular string is repeating more than once in each line?


I have a file like below (an example file).

10,Bob,elec,Bob,hero,,

20,Bob,mech,steve,juni,,,yumm

30,Bob,instr,Bob,sen

40,Bob,comps,alex,juni,syu,,

50,Bob,chem,Bob,Bob,seni

I would need all lines where string Bob is appearing more than once in each line.

Also if its not a delimited file as above and if it is a file like below then how would i get the lines where a particular string is repeating(In this case Bob)

10,Bob,elecBob,hero,,

20,Bob,mech,steve,juni|||Bob,yummBob

30,BobExtarvagnaxz|||

I tried couple of options but not ending up what exactly required.

Thanks Chethan


Solution

  • line starting with 20 in your output is not same as 20 in input. And why 30,50 in input are not in output? they have more than one "Bob".

    anyway, try if this quick and dirty line works for u.

     awk '/.*(Bob).*(Bob).*/' yourFile