2

Consider the below configuration file and it has two instances of alt_names. Here, I want to delete a line with second occurrence of a pattern alt_names.

.
.
[ server ]
subjectAltName = @alt_names
.
.

[ alt_names ]

I tried sed command but it deletes all the lines with the pattern occurrence. I used the below command to do so,

#sed '/alt_names/{2,$d}' inputfile

Can some shed lights on whats gone wrong the usage of sed here.

Panch
  • 985
  • 2
  • 11
  • 36

1 Answers1

1

Following will delete the second line which contains alt_names. Counter c will increase with each line having the pattern matched.

awk '/alt_names/{c++;if(c==2)gsub(/^.*$/,"")}1'
.
[ server ]
subjectAltName = @alt_names
.
.

To modify and save:

awk '/alt_names/{c++;if(c==2)gsub(/^.*$/,"")}1' inputfile >inputfile.tmp && mv inputfile.tmp inputfile
P....
  • 14,772
  • 2
  • 24
  • 42