0

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

Community
  • 1
  • 1
Chkusi
  • 139
  • 1
  • 5
  • 15

4 Answers4

1

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
Kent
  • 189,393
  • 32
  • 233
  • 301
  • Thanks for the help!! However there is no input and output in my question. Its 2 different versions of input files. – Chkusi Oct 13 '11 at 13:07
  • oh, i thought the 2nd code block is your expecting output. ^_^ – Kent Oct 13 '11 at 14:32
1

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
1

This will delete any lines not having multiple Bobs.

sed '/Bob.*Bob/!d' filename
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

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
ktf
  • 6,865
  • 1
  • 13
  • 6