Celeb Glow
general | March 24, 2026

Grepping all strings on the same line

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 Answer

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".

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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