1

I want to use grep command to extract some strings from my data file.

data file format

a=1,b=2,c=3,
a=4,b=5,c=6,

I want the out format

a=1,c=3,
a=4,c=6,

I tried the below command

grep -Po 'a=.*?,|c=.*?,' data

But the output format is incorrect. Please teach me how to do it.

2
  • I suggest to use cut or awk or sed. – Cyrus Jan 6 '18 at 15:22
  • 1
    awk -F"," '{ print $1","$3"," }' – olivierg Jan 6 '18 at 20:29
0

One possibility with sed would be as follows.

sed -n 's/\(a=.\?,\)\(.*\)\(c=.\?\)/\1\3/p' data

A more generic regex would be as follows.

sed -n 's/\([a-zA-Z]=.\?,\)\([a-zA-Z]=.\?,\)\([a-zA-Z]=.\?,\)/\1\3/p' input
1
  • or sed 's/,[^,]\+//' file – Cyrus Jan 6 '18 at 18:27

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.