0

Trying to find a way to grep all names on one line for 100 files. grepping all names available in each file must appear on the same line.

FILE1

"company":"COMPANY1","companyDisplayName":"CM1","company":"COMPANY2","companyDisplayName":"CM2","company":"COMPANY3","companyDisplayName":"CM3",

FILE2

"company":"COMPANY99","companyDisplayName":"CM99"

The output i actually want is, ( include file name as prefix.)

FILE1:COMPANY1,COMPANY2,COMPANY3    
FILE2:COMPANY99

i tried grep -oP '(?<="company":")[^"]*' * but i get results like this :

FILE1:COMPANY1
FILE1:COMPANY2
FILE1:COMPANY3
FILE2:COMPANY99
1

Since you're already using a Perl-compatible regular expression (PCRE) why not use Perl itself?

$ perl -0777 -lnE 'say "$ARGV:", join ",", /(?<="company":")[^"]*/g' FILE*
FILE1:COMPANY1,COMPANY2,COMPANY3
FILE2:COMPANY99

If each file contains only a single line, you may omit the -0777 "slurp".

  • Thank a lot mr @steeldriver its working fine – H.Otmane Mar 10 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.