ʞɔıu
ʞɔıu

Reputation: 48396

Match specific lines in sed command

How can I match a set of specific lines for a substitution command?

(incorrect):

sed -e'71,116,211s/[ ]+$//' ...

I want to strip trailing whitespace on lines 71, 116 and 211 only

Upvotes: 2

Views: 167

Answers (3)

potong
potong

Reputation: 58371

This might work for you (GNU sed):

sed -r '/\s+$/!b;71s///;116s///;221s///' file

or perhaps:

sed -e '/  *$/!b' -e '71s///' -e '116s///' -e '221s///' file

or as has been said already:

sed -e '71ba' -e '116ba' -e '221ba' -e 'b' -e ':a' -e 's/  *$//' file

Upvotes: 0

Ian Kenney
Ian Kenney

Reputation: 6426

You could try something like:

awk 'NR== 71 || NR == 116 || NR == 211 {sub(/ *$/,"",$0)}{print $0}'

or

sed '71s/ *$//;116s///;211s///'

Upvotes: 1

perreal
perreal

Reputation: 97918

sed '71bl;116bl;211bl;b;:l;s/[ ][ ]*$//' input

For any specified line, this script jumps to the label l. Other lines will jump to the end of the script with the bare branch.

And an awk solution:

awk -v k="71,116,221" 'BEGIN{split(k,a,",")} 
            (NR in a) { sub(/ *$/,"",$0) }1' input

Upvotes: 1

Related Questions