ghaschel
ghaschel

Reputation: 1355

Sed replace pattern with line number

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

Upvotes: 10

Views: 11332

Answers (7)

potong
potong

Reputation: 58558

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//'

Upvotes: 4

Ben Page
Ben Page

Reputation: 3100

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/

Upvotes: 0

simyou
simyou

Reputation: 111

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

Upvotes: 1

Thor
Thor

Reputation: 47219

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

Upvotes: 3

twalberg
twalberg

Reputation: 62499

Simple awk oneliner:

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

Upvotes: 10

Lev Levitsky
Lev Levitsky

Reputation: 65851

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

Upvotes: 5

Kent
Kent

Reputation: 195229

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

Upvotes: 1

Related Questions