Search code examples
sedawkgawk

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


Solution

  • Simple awk oneliner:

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