PDA

View Full Version : sed non-sequential line numbers



maclenin
January 6th, 2013, 07:21 PM
I am not quite sure how to sed this:

Substitue "A" for "B" in lines 5 and 11 and 34 and 92 and 108 within test_file.

I am able to substitue "A" for "B" in a range of lines within test_file:

sed -r '5,108 s/B/A/' -i test_file

I am also able to subsitute "A" for "B" in a single line within test_file:

sed -r '34 s/B/A/' -i test_file

Thanks for any guidance!

ibuclaw
January 6th, 2013, 07:53 PM
I am not quite sure how to sed this:

Substitue "A" for "B" in lines 5 and 11 and 34 and 92 and 108 within test_file.

I am able to substitue "A" for "B" in a range of lines within test_file:

sed -r '5,108 s/B/A/' -i test_file

I am also able to subsitute "A" for "B" in a single line within test_file:

sed -r '34 s/B/A/' -i test_file

Thanks for any guidance!

Don't think it can be done without using -e for each regexp.


-e '5 s/B/A' -e '11 s/B/A' -etc...

Vaphell
January 6th, 2013, 08:02 PM
if you want to keep it short and not duplicate the expression you could do something like this:

$ s='s/A/A*/'
$ for i in {1..10}; do echo A; done | sed "5$s; 7$s; 9$s"
A
A
A
A
A*
A
A*
A
A*
A