I have a file where certain (not all) lines look like so:
Sequence: n
I wish to replace n
with the respective line number.
How can I do it with sed?
I have a file where certain (not all) lines look like so:
Sequence: n
I wish to replace n
with the respective line number.
How can I do it with sed?
It is very simple with awk
(thanks to @glenn jackman):
awk '/Sequence: n/ { $2 = NR } { print }' input-file
Regarding sed
, I've only been able to do this with a two-pass elaboration (tested with GNU sed
and with ssed
:):
sed '/^Sequence: n$/{s/n$//p;=;d;}' | sed '/^Sequence: $/{N;s/\n//;}'
The first inserts the line number in the row after "Sequence: ", the second joins the two lines.
awk '/Sequence: n/ {$2 = NR} {print}'
Commented
Aug 25, 2011 at 21:22