Skip to content

Instantly share code, notes, and snippets.

@emanuele6
Last active March 2, 2025 20:59
Show Gist options
  • Save emanuele6/d846879d025fbbac674b676df6682b70 to your computer and use it in GitHub Desktop.
Save emanuele6/d846879d025fbbac674b676df6682b70 to your computer and use it in GitHub Desktop.
POSIX ed script that prepends line numbers to the lines of a file.
1s/^/1 /
2,$g/^/-t -\
s/ .*//\
s/^9*$/0&/\
t .\
s/^.*[^9]\(9*\)$/\1 /\
s/9/0/g\
-s/9*$//\
s/8$/9/\
s/7$/8/\
s/6$/7/\
s/5$/6/\
s/4$/5/\
s/3$/4/\
s/2$/3/\
s/1$/2/\
s/0$/1/\
.,+2j
w
q
@dmbarrad
Copy link

dmbarrad commented Feb 9, 2024

Thanks a lot, this is genius. I love it.
Thank you for the script and for the explanation.
i couldn't understand it specifically because of the 9 replaced by 0 part and copying another line and the effect it had in the incrementation process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment