awk '{print NR": "$0}' < inputfile
一样,在某些Unix版本上,cat命令有-n选项,或是more(less, pg)也有能显示行号的选项。但还有一些Unix的版本上,可能这些都通通没有,那么此时,下面的这个简易脚本就能够发威了: shell
#!/bin/sh # numberlines.sh -- 效果等同于cat -n命令 for filename do linecount="1" while read line do echo "${linecount}: $line" linecount="$(($linecount+1))" done < $filename done exit 0这个脚本能够接受任意多的文件做为输入,可是不能经过管道给它提供输入。固然,如有须要能够修改下程序。
$ numberlines text.snippet.txt 1: Perhaps one of the most valuable uses of shell scripts is to fix 2: your particular flavor of Unix and make it more like other flavors, 3: to bring your commands into conformance or to increase consistency 4: across different systems. The outsider view of Unix suggests a 5: nice, uniform command-line experience, helped along by the existence 6: of and compliance with the POSIX standards for Unix. But anyone who's 7: ever touched more than one computer knows how much they can vary 8: within these broad parameters.若是你有了一个标记出行号的文件,那么此时你就能够反转文件了:
cat -n filename | sort -nr | cut -c8-上面的这行命令何时会颇有用呢?一个常常遇到的状况就是,你想显示一个日志文件的时候,