Sed replace pattern with line number

喜欢而已 提交于 2019-12-01 04:01:11

Simple awk oneliner:

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

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

This might work for you (GNU sed):

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

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

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

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!