示例:
假设 nowcoder.txt 内容如下:
a b c d e f你的脚本应当输出:
3 5 7 9 10
i=1;while read p; do if [[ $p == '' ]]; then echo $i; fi; ((i++)); done < nowcoder.txt i=1;while read p; do if [ -z $p ]; then echo $i; fi; ((i++)); done < nowcoder.txt awk '/^$/ {print NR}' nowcoder.txt sed -n '/^$/=' nowcoder.txt grep -n '^$' nowcoder.txt | awk -F: '{print $1}' grep -n "^$" nowcoder.txt | sed -n 's/\://p' grep -n "^$" nowcoder.txt | sed 's/\://g' grep -n "^$" nowcoder.txt | sed "s@:@@g"
#!/bin/bash #利用grep查找空行并打印(有冒号),然后用sed把冒号替换成空并打印 grep -n "^ $" nowcoder.txt | sed -n 's@\:@@p'
#!/bin/bash i=1 while read line do if [ -z $line ] then echo $i fi ((i++)) done < nowcoder.txt