去掉文件的空行

去掉文件的空行

写一个 bash脚本以去掉一个文本文件nowcoder.txt中的空行
示例:
假设nowcoder.txt 内容如下:

1
2
3
4
5
6
7
8
9
10
11
abc

567


aaa
bbb



ccc

你的脚本应当输出:

1
2
3
4
5
abc
567
aaa
bbb
ccc

题解

1
2
3
4
5
6
7
8
9
10
11
# 方法 1
sed -n '/^$/!p' nowcoder.txt

# 方法2
awk -n '{if ($0 != "") print $0}' nowcoder.txt

# 方法 3
awk '!/^$/ {print $0}' nowcoder.txt

# 方法 4
grep -v '^$' nowcoder.txt

(本文完)

在线题目


去掉文件的空行
https://maojun.xyz/blog/2023/11/去掉文件的空行.html
作者
毛 俊
发布于
2023年11月7日
许可协议