Skip to main content
added 183 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

sed is a Turing complete language, it can do anything. Someone even implemented dc, the Unix direct calculator in sed.

Compared to that, getting the line number would be trivial, but while sed's language has ready support to match on line number (like with 12,21 { actions; } to run actions on lines 12 to 21) or print the line number (with =), none of those would help if you wanted to insert that line number in the result of a substitution for instance or more generally insert it in the hold or pattern space.

Short of piping the output of = in a separate sed invocation as shown by @pLumo, you're left with incrementing a number by yourself using the kind of technique used by dc.sed like in:

sed 'x;:1 s/$/,,0123456789,0/;s/^,/0,/ s/\(.\),\([^,]*\).*\1\(,*.\).*/\3\2/;/,/b1 x;G;s/\(.*\)\n\(.*\)/line \2 is \1/' 

Remember that sed was a tool written in the 70s on computers with very limited resources. It was mostly made obsolete by awk in the late 70s and of course fully by perl in the late 80s where those things are trivially done:

awk '{print "line "NR" is "$0}' perl -ne 'print "line $. is $_"' 
awk '{$0 = "line "NR" is "$0};1' perl -pe '$_ = "line $. is $_"' 

Or even more sed-like:

awk '{sub(/^/, "line "NR" is ")};1' perl -pe 's/^/line $. is /' 

sed is a Turing complete language, it can do anything. Someone even implemented dc, the Unix direct calculator in sed.

Compared to that, getting the line number would be trivial, but while sed's language has ready support to match on line number (like with 12,21 { actions; } to run actions on lines 12 to 21) or print the line number (with =), none of those would help if you wanted to insert that line number in the result of a substitution for instance or more generally insert it in the hold or pattern space.

Short of piping the output of = in a separate sed invocation as shown by @pLumo, you're left with incrementing a number by yourself using the kind of technique used by dc.sed like in:

sed 'x;:1 s/$/,,0123456789,0/;s/^,/0,/ s/\(.\),\([^,]*\).*\1\(,*.\).*/\3\2/;/,/b1 x;G;s/\(.*\)\n\(.*\)/line \2 is \1/' 

Remember that sed was a tool written in the 70s on computers with very limited resources. It was mostly made obsolete by awk in the late 70s and of course fully by perl in the late 80s where those things are trivially done:

awk '{print "line "NR" is "$0}' perl -ne 'print "line $. is $_"' 

sed is a Turing complete language, it can do anything. Someone even implemented dc, the Unix direct calculator in sed.

Compared to that, getting the line number would be trivial, but while sed's language has ready support to match on line number (like with 12,21 { actions; } to run actions on lines 12 to 21) or print the line number (with =), none of those would help if you wanted to insert that line number in the result of a substitution for instance or more generally insert it in the hold or pattern space.

Short of piping the output of = in a separate sed invocation as shown by @pLumo, you're left with incrementing a number by yourself using the kind of technique used by dc.sed like in:

sed 'x;:1 s/$/,,0123456789,0/;s/^,/0,/ s/\(.\),\([^,]*\).*\1\(,*.\).*/\3\2/;/,/b1 x;G;s/\(.*\)\n\(.*\)/line \2 is \1/' 

Remember that sed was a tool written in the 70s on computers with very limited resources. It was mostly made obsolete by awk in the late 70s and of course fully by perl in the late 80s where those things are trivially done:

awk '{print "line "NR" is "$0}' perl -ne 'print "line $. is $_"' 
awk '{$0 = "line "NR" is "$0};1' perl -pe '$_ = "line $. is $_"' 

Or even more sed-like:

awk '{sub(/^/, "line "NR" is ")};1' perl -pe 's/^/line $. is /' 
added 10 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

sed is a Turing complete language, it can do anything. Someone even implemented dc, the Unix direct calculator in sed.

Compared to that, getting the line number would be trivial, but while sed's language has ready support to match on line number (like with 12,21 { actions; } to run actions on linelines 12 to 21) or print the line number (with =), none of those would help if you wanted to insert that line number in the result of a substitution for instance or more generally insert it in the hold or pattern space.

Short of piping the output of = in a separate sed invocation as shown by @pLumo, you're left with incrementing a number by yourself using the kind of technique used by dc.sed like in:

sed 'x;:1 s/$/,,0123456789,0/;s/^,/0,/ s/\(.\),\([^,]*\).*\1\(,*.\).*/\3\2/;/,/b1 x;G;s/\(.*\)\n\(.*\)/line \2 is \1/' 

Remember that sed was a tool written in the 70s on computers with very limited resources. It was mostly made obsolete by awk in the late 70s and of course fully by perl in the late 80s where those things are trivially done:

awk '{print "line "NR" is "$0}' perl -ne 'print "line $. is $_"' 

sed is a Turing complete language, it can do anything. Someone even implemented dc, the Unix direct calculator in sed.

Compared to that, getting the line number would be trivial, but while sed's language has ready support to match on line number (like with 12 { actions; } to run actions on line 12) or print the line number (with =), none of those would help if you wanted to insert that line number in the result of a substitution for instance or more generally insert it in the hold or pattern space.

Short of piping the output of = in a separate sed invocation as shown by @pLumo, you're left with incrementing a number by yourself using the kind of technique used by dc.sed like in:

sed 'x;:1 s/$/,,0123456789,0/;s/^,/0,/ s/\(.\),\([^,]*\).*\1\(,*.\).*/\3\2/;/,/b1 x;G;s/\(.*\)\n\(.*\)/line \2 is \1/' 

Remember that sed was a tool written in the 70s on computers with very limited resources. It was mostly made obsolete by awk in the late 70s and of course fully by perl in the late 80s where those things are trivially done:

awk '{print "line "NR" is "$0}' perl -ne 'print "line $. is $_"' 

sed is a Turing complete language, it can do anything. Someone even implemented dc, the Unix direct calculator in sed.

Compared to that, getting the line number would be trivial, but while sed's language has ready support to match on line number (like with 12,21 { actions; } to run actions on lines 12 to 21) or print the line number (with =), none of those would help if you wanted to insert that line number in the result of a substitution for instance or more generally insert it in the hold or pattern space.

Short of piping the output of = in a separate sed invocation as shown by @pLumo, you're left with incrementing a number by yourself using the kind of technique used by dc.sed like in:

sed 'x;:1 s/$/,,0123456789,0/;s/^,/0,/ s/\(.\),\([^,]*\).*\1\(,*.\).*/\3\2/;/,/b1 x;G;s/\(.*\)\n\(.*\)/line \2 is \1/' 

Remember that sed was a tool written in the 70s on computers with very limited resources. It was mostly made obsolete by awk in the late 70s and of course fully by perl in the late 80s where those things are trivially done:

awk '{print "line "NR" is "$0}' perl -ne 'print "line $. is $_"' 
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

sed is a Turing complete language, it can do anything. Someone even implemented dc, the Unix direct calculator in sed.

Compared to that, getting the line number would be trivial, but while sed's language has ready support to match on line number (like with 12 { actions; } to run actions on line 12) or print the line number (with =), none of those would help if you wanted to insert that line number in the result of a substitution for instance or more generally insert it in the hold or pattern space.

Short of piping the output of = in a separate sed invocation as shown by @pLumo, you're left with incrementing a number by yourself using the kind of technique used by dc.sed like in:

sed 'x;:1 s/$/,,0123456789,0/;s/^,/0,/ s/\(.\),\([^,]*\).*\1\(,*.\).*/\3\2/;/,/b1 x;G;s/\(.*\)\n\(.*\)/line \2 is \1/' 

Remember that sed was a tool written in the 70s on computers with very limited resources. It was mostly made obsolete by awk in the late 70s and of course fully by perl in the late 80s where those things are trivially done:

awk '{print "line "NR" is "$0}' perl -ne 'print "line $. is $_"'