Sed replace pattern with line number

This might work for you (GNU sed):

sed = file | sed 'N;:a;s/\(\(.*\)\n.*\)###/\1\2/;ta;s/.*\n//'

An alternative using cat:

cat -n file | sed -E ':a;s/^(\s*(\S*)\t.*)###/\1\2/;ta;s/.*\t//'

Simple awk oneliner:

awk '{gsub("###",NR,$0);print}'

As noted by Lev Levitsky this isn't possible with one invocation of sed, because the line number is sent directly to standard out.

You could have sed write a sed-script for you, and do the replacement in two passes:

infile

a
b
c
d
e
###
###
###
a
b
###
c
d
e
###

Find the lines that contain the pattern:

sed -n '/###/=' infile

Output:

6
7
8
11
15

Pipe that into a sed-script writing a new sed-script:

sed 's:.*:&s/###/&/:'

Output:

6s/###/6/
7s/###/7/
8s/###/8/
11s/###/11/
15s/###/15/

Execute:

sed -n '/###/=' infile | sed 's:.*:&s/^/& \&/:' | sed -f - infile

Output:

a
b
c
d
e
6
7
8
a
b
11
c
d
e
15

Given the limitations of the = command, I think it's easier to divide the job in two (actually, three) parts. With GNU sed you can do:

$ sed -n '/###/=' test > lineno

and then something like

$ sed -e '/###/R lineno' test | sed '/###/{:r;N;s/###\([^\n]*\n\)\([^\n]*\)/\2\1/;tr;:c;s/\n\n/\n/;tc}'

I'm afraid there's no simple way with sed because, as well as the = command, the r and GNU extension R commands don't read files into the pattern space, but rather directly append the lines to the output, so the contents of the file cannot be modified in any way. Hence piping to another sed command.

If the contents of test are

fooo
bar ### aa
test
zz ### bar

the above will produce

fooo
bar 2 aa
test
zz 4 bar

Tags:

Awk

Sed

Gawk