Sed replace pattern with line number

2019-04-26 09:23发布

问题:

I need to replace the pattern ### with the current line number.

I managed to Print in the next line with both AWK and SED.

sed -n "/###/{p;=;}" file prints to the next line, without the p;, it replaces the whole line.

sed -e "s/###/{=;}/g" file used to make sense in my head, since the =; returns the line number of the matched pattern, but it will return me the the text {=;}

What am i Missing? I know this is a silly question. I couldn't find the answer to this question in the sed manual, it's not quite clear.

If possible, point me what was i missing, and what to make it work. Thank you

回答1:

Simple awk oneliner:

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


回答2:

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


回答3:

This might work for you (GNU sed):

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


回答4:

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


回答5:

is this ok ?

kent$  echo "a
b
c
d
e"|awk '/d/{$0=$0" "NR}1'
a
b
c
d 4
e

if match pattern "d", append line number at the end of the line.

edit

oh, you want to replace the pattern not append the line number... take a look the new cmd:

kent$  echo "a
b
c
d
e"|awk '/d/{gsub(/d/,NR)}1'
a
b
c
4
e

and the line could be written like this as well: awk '1+gsub(/d/,NR)' file



回答6:

one-liner to modify the FILE in place, replacing LINE with the corresponding line number:

seq 1 `wc -l FILE | awk '{print $1}'` | xargs -IX sed -i 'X s/LINE/X/' FILE


标签: sed awk gawk