示例:
假设 nowcoder.txt 内容如下:
#include <iostream> using namespace std; int main() { int a = 10; int b = 100; cout << "a + b:" << a + b << endl; return 0; }你的脚本应当输出:
9
#include <iostream> using namespace std; int main() { int a = 10; int b = 100; cout << "a + b:" << a + b << endl; return 0; }你的脚本应当输出:
#include <iostream> using namespace std; int main() { int a = 10; int b = 100; cout << "a + b:" << a + b << endl; return 0; }
9
grep -n "" nowcoder.txt|awk -F: '{print }'|tail -n1| awk -F: '{print $1}' awk '{print NR}' nowcoder.txt|tail -n1 awk 'END{print NR}' nowcoder.txt sed -n '$=' nowcoder.txt cat nowcoder.txt |wc -l以上5种方法均可
cat nowcoder.txt | wc -l wc -l < nowcoder.txt wc -l nowcoder.txt | gawk '{print $1}'
grep -n "" nowcoder.txt | tail -n1 | gawk -F: '{print $1}'
sed -n '$=' nowcoder.txt
gawk '{print NR}' nowcoder.txt | tail -n1 gawk 'END{print NR}' nowcoder.txt
wc [选项] [文件]...常见选项:
#!/bin/bash wc -l < nowcoder.txt此外若使用wc命令没有文件参数,那么将从标准输入读取数据:
#!/bin/bash cat nowcoder.txt|wc -l
#!/bin/bash awk 'BEGIN {lines=0;}{lines=lines+1;} END {printf("%d\n",lines)}' nowcoder.txt