如何通过Shell脚本自动追踪并报告文件中的空行行号?
- 内容介绍
- 文章标签
- 相关推荐
本文共计195个文字,预计阅读时间需要1分钟。
1. 使用bash脚本输出文本文件`nowcoder.txt`中空行的行号,可能连续,从1开始。
bashawk 'NF==0 {print NR}' nowcoder.txt
2. 使用bash脚本输出文本文件`nowcoder.txt`中所有行的行号,可能连续,从1开始。
bashawk '{print NR}' nowcoder.txt
1. 写一个 bash脚本以输出一个文本文件 nowcoder.txt中空行的行号,可能连续,从1开始。
awk '/^\s*$/{print NR}' nowcoder.txt
2. 去掉文件的空行输出
方法1:
awk '{if($0 != "") {print $0}}' ./nowcoder.txtawk '!/^$/ {print $NF}'
方法2:
cat ./nowcoder.txt | awk NF
方法3: -v 显示不包含匹配文本的所有行
grep -v '^$'
-e 指定字符串做为查找文件内容的样式
grep -e '\S'
本文共计195个文字,预计阅读时间需要1分钟。
1. 使用bash脚本输出文本文件`nowcoder.txt`中空行的行号,可能连续,从1开始。
bashawk 'NF==0 {print NR}' nowcoder.txt
2. 使用bash脚本输出文本文件`nowcoder.txt`中所有行的行号,可能连续,从1开始。
bashawk '{print NR}' nowcoder.txt
1. 写一个 bash脚本以输出一个文本文件 nowcoder.txt中空行的行号,可能连续,从1开始。
awk '/^\s*$/{print NR}' nowcoder.txt
2. 去掉文件的空行输出
方法1:
awk '{if($0 != "") {print $0}}' ./nowcoder.txtawk '!/^$/ {print $NF}'
方法2:
cat ./nowcoder.txt | awk NF
方法3: -v 显示不包含匹配文本的所有行
grep -v '^$'
-e 指定字符串做为查找文件内容的样式
grep -e '\S'

