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

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

问题描述

从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.

推荐答案

使用 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天全站免登陆