How to use the line number with sed?
This question is about understanding, not about completing a specific task.
And, no, Sed to print out the line number has been proposed a second time and does not answer this question better than the first time. I'm specially asking how to access the line number, as printed with = inside a single call of sed. If I really have to use pipes, there are better options then sed '=' , that would include grep -e -n.
Widespread believe is that sed cannot count, but this is wrong.
I have seen https://unix.stackexchange.com/a/707343 and https://linux.die.net/man/1/sed so there is the = command that prints the line number.
What I want to know is, if that line number can be used inside of a single sed call, for example in a replacement string, something like sed 's/^/=&/' which doesn't work as desired but takes the equal sign literally.
I wonder if this is really not possible, or if I just miss the proper syntax.
I'm aware that grep -n -e does the trick, but I would like to know if it is possible with sed alone, in one single call, without pipes, tr, or other helpers.
The main objective is to learn how to handle line numbers with sed,in more complex ways.
Quite obviously, sed can count, otherwise address ranges based on numbers would not be possible. It boils down on how to access the line number.
As for the suggested (false) duplicate Sed to print out the line number my question, as stated above, is about using the line numbers within sed.
sed '=' myfile | sed 'N;s/\n/ /'sedspecifically to add the line number to the beginning of the line" but is more asking the broader, more general "I know that=can be used insedto do things with the line number, so what kind of things can it do?". Gyro, it would help if you could tidy up a bit, remove unnecessarily combative words like "superstition" and just ask how=can be used insedfor anything more complex.sed '='=awk '{print NR ORS $0}', and what I think you're trying to do withsed 's/^/=&/'would beawk '{print NR $0}'). Any time you find yourself attempting to use sed constructs other than s, g, and p (with -n) take a second to think about what you're doing as it'd probably be some combination of clearer, simpler, more robust, more efficient and/or more portable with awk.