January 2004
Intermediate to advanced
368 pages
7h 25m
English
There are a lot of ways to add line numbers to a displayed file, many of which are quite short. Here's a solution using awk:
awk '{ print NR": "$0 }' < inputfile
On some Unix implementations, the cat command has an -n flag, and on others, the more (or less, or pg) pager has a flag for specifying that each line of output should be numbered. But on some Unixes, none of these will work, in which case the simple script given here can do the job.
#!/bin/sh
# numberlines - A simple alternative to cat -n, etc.
for filename
do
linecount="1"
while read line
do
echo "${linecount}: $line"
linecount="$(( $linecount + 1 ))"
done < $filename
done
exit 0
You can feed as many filenames ...
Read now
Unlock full access