如何从文本文件中的第一个空行开始删除所有行? [英] How to remove all lines from a text file starting at first empty line?

查看:8
本文介绍了如何从文本文件中的第一个空行开始删除所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Bash 中的第一个空行开始从文本文件中删除所有行的最佳方法是什么?可以使用外部工具(awk、sed...)!

What is the best way to remove all lines from a text file starting at first empty line in Bash? External tools (awk, sed...) can be used!

示例

1: ABC
2: DEF
3:
4: GHI

应删除第 3 行和第 4 行,并将剩余内容保存在新文件中.

Line 3 and 4 should be removed and the remaining content should be saved in a new file.

推荐答案

With AWK:

$ awk '/^$/{exit} 1' test.txt > output.txt

output.txt

$ cat output.txt 
ABC
DEF

演练:对于匹配 ^$(行首、行尾)的行,退出(整个脚本).对于所有行,打印整行——当然,在一行让我们退出后,我们不会到达这一部分.

Walkthrough: For lines that matches ^$ (start-of-line, end-of-line), exit (the whole script). For all lines, print the whole line -- of course, we won't get to this part after a line has made us exit.

这篇关于如何从文本文件中的第一个空行开始删除所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆