10

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

ghaschel
  • 1,313
  • 3
  • 20
  • 41

7 Answers7

10

Simple awk oneliner:

awk '{gsub("###",NR,$0);print}'
twalberg
  • 59,951
  • 11
  • 89
  • 84
  • @Thor Maybe I'm making different assumptions - doesn't that just put the line number at the beginning of the line? I read the OP to mean that "###" might be anywhere in the line, hence the `gsub()`. Although on 2nd reading, it does seem having the number at beginning of line might be what's in mind... – twalberg Sep 19 '12 at 15:49
5

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
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
4

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//'
potong
  • 55,640
  • 6
  • 51
  • 83
3

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
Thor
  • 45,082
  • 11
  • 119
  • 130
1

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

Kent
  • 189,393
  • 32
  • 233
  • 301
1

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
simyou
  • 111
  • 1
  • for osx version of sed I had to modify this as the `-i` param is treated differently https://stackoverflow.com/a/63854797/29924 – Ben Page Sep 11 '20 at 22:00
0

Following on from https://stackoverflow.com/a/53519367/29924

If you try this on osx the version of sed is different and you need to do:

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

see https://markhneedham.com/blog/2011/01/14/sed-sed-1-invalid-command-code-r-on-mac-os-x/

Ben Page
  • 3,011
  • 4
  • 35
  • 37