首页 > 试题广场 >

去掉空行

[编程题]去掉空行
  • 热度指数:41216 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
写一个 bash脚本以去掉一个文本文件nowcoder.txt中的空行
示例:
假设nowcoder.txt 内容如下:
abc

567


aaa
bbb



ccc
你的脚本应当输出:
abc
567
aaa
bbb
ccc
示例1

输入

abc

567


aaa
bbb



ccc

输出

abc
567
aaa
bbb
ccc
grep . 
发表于 2021-07-12 01:16:13 回复(7)
cat nowcoder.txt | awk NF
发表于 2021-01-08 10:55:34 回复(1)
sed -n '/[^$]/p'
sed -n 静默模式
p:打印变动的流(行)
正则部分: [^$] ^代表以后面跟着的字符为开头,$代表以前面的字符为结尾;
^$联合使用,中间不加任何字符数字,代表匹配空行;
[ ] 在shell正则中表示取反

发表于 2021-02-19 17:11:53 回复(3)
while read line
do
    if [ -z $line ]
    then
        continue
    fi
    echo $line
done

发表于 2021-04-09 16:38:41 回复(1)
sed '/^$/d' nowcoder.txt
发表于 2020-12-23 08:31:42 回复(0)
grep -v '^$' nowcoder.txt
发表于 2021-06-23 16:14:11 回复(0)
awk NF nowcoder.txt
发表于 2022-01-14 15:46:28 回复(0)
awk 'NF!=0{print}'
发表于 2021-11-28 21:41:51 回复(0)
# grep . nowcoder.txt
awk '$0!=null {print $0}' nowcoder.txt
发表于 2021-08-21 22:27:28 回复(1)
cat nowcoder.txt | awk NF
NF:只会记录有数据的行
发表于 2021-07-16 14:22:42 回复(0)


 grep -v '^$' 
 grep -e '\S+'
 grep -E  '\S+'
 

sed -n '/[^$]/p'



awk NF # ?
awk '!/^$/{print $NF}' 

发表于 2021-04-10 11:44:44 回复(2)
grep -v -E "^$" txt
发表于 2021-01-14 23:45:36 回复(0)
我建议把题目改成输出文件除空行外的内容。
按照本题的意思本来应该用sed -i '/^$/d' nowcoder.txt
但实际上用 grep -v "^$" nowcoder.txt  才能通过题目


发表于 2023-07-12 21:28:04 回复(0)
sed '/^$/d'
awk '!/^$/{print}'


发表于 2022-05-09 11:36:43 回复(0)
题目不是要去掉文件中的空格行吗?为啥看你们答案只是展示的结果去除了空白行,原文件的空白行并没有去除🙄
发表于 2021-09-07 16:49:56 回复(1)
sed '/^$/d' nowcoder.txt
人狠话不多
发表于 2021-07-28 17:26:00 回复(0)
sed '/^$/d'
发表于 2021-07-09 17:00:35 回复(0)
cat nowcoder.txt | sed '/^$/Id'
发表于 2021-03-08 02:28:53 回复(0)
awk '!/^$/ {print $NF}' nowcoder.txt
发表于 2021-01-22 16:39:19 回复(0)
sed '/^$/d' nowcoder.txt
发表于 2024-03-27 00:32:55 回复(0)