Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed replace pattern with line number

Tags:

sed

awk

gawk

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

like image 229
ghaschel Avatar asked Sep 19 '12 14:09

ghaschel


1 Answers

Simple awk oneliner:

awk '{gsub("###",NR,$0);print}'
like image 108
twalberg Avatar answered Nov 26 '22 06:11

twalberg