So, an aquaintance recently discovered /usr/bin/nl after struggling with paste and seq to get number lines for a file. I mentioned 'Do you know cat -n? There was an article in the 80s about how those switches added to cat were anti-Unix' (Note: Found at http://harmful.cat-v.org/cat-v/unix_prog_design.pdf, on the "bloated" 4.2BSD version). And then I tried to come up with several different ways to do it, and thought up several.
I'm primarily a Linux user, so that's what I'm doing, but I'll test it in some other old and new OS's. Anyone have access to AIX or HP-UX?
Just going to use some temporary output:
There's the obvious:$ /etc/update-motd.d/50-landscape-sysinfo > text $ cat text System information as of Sat Dec 2 11:41:44 CST 2017 System load: 0.06 Processes: 182 Usage of /: 48.3% of 46.24GB Users logged in: 1 Memory usage: 27% IP address for eth0: 172.16.0.4 Swap usage: 0% IP address for docker0: 172.17.0.1 Graph this data and manage this system at: https://landscape.canonical.com/
nl method tested:$ which nl /usr/bin/nl $ nl text 1 System information as of Sat Dec 2 11:41:44 CST 2017 2 System load: 0.06 Processes: 182 3 Usage of /: 48.3% of 46.24GB Users logged in: 1 4 Memory usage: 27% IP address for eth0: 172.16.0.4 5 Swap usage: 0% IP address for docker0: 172.17.0.1 6 Graph this data and manage this system at: 7 https://landscape.canonical.com/
nl method tested:$ nl -ba text 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at:
cat -n method tested:$ which cat /bin/cat $ cat -n text 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/
grep -n method tested:$ grep -n ^ text | sed 's/:/ /' 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/
$ pr -t -n text 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/
awk var++ method tested:$ awk '{print ++n " " $0}' text 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/
awk NR method tested:$ awk '{print NR " " $0}' text 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/
ex method tested:$ printf 'set number\ng/^/p\n' | ex text 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/
(echo set number; echo "g/^/p") | ex text
ed method tested:$ echo '1,$n' | ed -s text 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/
sed method tested:$ sed = text | paste - - 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/
ksh/bash method tested:$ paste <(seq $(wc -l < text)) text 1 2 System information as of Sat Dec 2 11:41:44 CST 2017 3 4 System load: 0.06 Processes: 182 5 Usage of /: 48.3% of 46.24GB Users logged in: 1 6 Memory usage: 27% IP address for eth0: 172.16.0.4 7 Swap usage: 0% IP address for docker0: 172.17.0.1 8 9 Graph this data and manage this system at: 10 https://landscape.canonical.com/