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.

Cyrus
  • 927
  • 1
  • 7
  • 15
tomli
  • 21
  • 5

1 Answers1

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
Thomas
  • 4,225
  • 5
  • 23
  • 28