Hello people,
I am trying with sed to insert some text at the beginning of each odd line of a file but no luck. Can you please help. Awk is also suitable but I am not very familiar with it.
Thank you in advance for any help.
Hello people,
I am trying with sed to insert some text at the beginning of each odd line of a file but no luck. Can you please help. Awk is also suitable but I am not very familiar with it.
Thank you in advance for any help.
What have you tried with sed?
Read in two lines at once with the "N" subcommand, then insert after the "\n", which is the newline separating the lines:
sed 'N;s/\n/&=changes here=/p'
In case of files with uneven numbers of lines you will have to introduce a special rule for the last line ("$"), otherwise the last line will not be printed.
I hope this helps.
bakunin
Something simpler and what you require:
cat testfile
ABCD
EFGH
IJKL
MNOP
PQRS
sed 's/.*/NEW TEXT &/;n' testfile
NEW TEXT ABCD
EFGH
NEW TEXT IJKL
MNOP
NEW TEXT PQRS
with awk..
awk 'NR%2{$0="some text "$0}1' file
Another sed variation - if you have GNU sed you could use an address with a step, e.g.
carlo@host:/tmp -> sed '1~2s/.*/NEW TEXT &/' testfile
NEW TEXT ABCD
EFGH
NEW TEXT IJKL
MNOP
NEW TEXT PQRS
Thank you all for your help.
Yet another sed variation:
sed 's/^/extra text /;n' infile