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

StackOverflow https://stackoverflow.com/questions/7754320

Question

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

Was it helpful?

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

OTHER TIPS

You can use a regex.

If you have a comma delimited file, you can look for one Bob, followed by another Bob, using:

grep -E "(^|,)Bob,.*Bob(,|$)" file.txt

If the file is not delimited, you can use a more general regex like:

grep -E "^.*Bob.*Bob.*$" file.txt

But this might not work correctly if you have a line like Bob,steve,Bobby because it won't know how to differentiate between Bob and Bobby.

This will delete any lines not having multiple Bobs.

sed '/Bob.*Bob/!d' filename

The following will print out every line where any 3-or more character string is repeated on the same line (if the string to be matched should be longer, just insert more dots within the brackets):

 $ grep '\(....*\).*\1' testdat

given the following input file:

 hahaAliAlihehe
 Ali ist allein
 Ali, Alibaba, Alimente
 Bert, Bertha, Bertram
 Holger, V'ger, Ludger
 Susi,Bernd,Holger

it prints:

 hahaAliAlihehe
 Ali, Alibaba, Alimente
 Bert, Bertha, Bertram
 Holger, V'ger, Ludger
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top