不定期更新…
1、统计文件的行数
方法1:
wc -l :查看文件的newline的数量。
linux中,newline字符就是 \n
字符。
$ wc -l ./nowcoder.txt
8 ./nowcoder.txt
输出会包含文件名,修改为:
¥wc -l ./nowcoder.txt | awk '{print $1}'
方法2:
#!/bin/bash
awk '{print NR}' ./nowcoder.txt |tail -n1
awk 可以打印所有行的行号:
$ awk '{print NR}' ./nowcoder.txt
该输出的最后一行就是文件的行数,结合 tail 就可以获取到文件的行数
$ awk '{print NR}' ./nowcoder.txt |tail -n 1
方法3:
awk 本身就可以只打印最后一行,因此一个 awk 脚本也可以实现
$ awk 'END{print NR}' ./nowcoder.txt
方法4:
使用 grep 搜索 “”,然后利用 grep 自带的功能统计行
$ grep -c "" ./nowcoder.txt
##或者
$ grep -n "" ./nowcoder.txt | awk -F ":" '{print $1 }' | tail -n 1
方法5:
sed统计行
$ sed -n '$=' ./nowcoder.txt