[英]How do I delete all lines from a file after (and including) a line that contains a defined string in a Bash script?
I'm hacking about a text file in the middle of a Bash script (on an RPI3B+ with OSMC installed) and trying to crop a file at the first line that contains the text "BLAH DE BLAH" (deleting everything in the same file after and including the first line it finds that text on). 我正在破解Bash脚本中间的文本文件(在安装了OSMC的RPI3B +上),并尝试在包含文本“ BLAH DE BLAH”的第一行中裁剪文件(此后删除同一文件中的所有内容)并在第一行中找到该文本)。
For example (in the file filename.text): 例如(在文件filename.text中):
This is the first line 这是第一行
This is the second line 这是第二行
This is the third line containing "BLAH DE BLAH" 这是包含“ BLAH DE BLAH”的第三行
This is the fourth line 这是第四行
This is the fifth line 这是第五行
Required output (in the file filename.text): 必需的输出(在文件filename.text中):
This is the first line 这是第一行
This is the second line 这是第二行
I've tried to investigate awk and sed related posts, but I'm finding it all so confusing as I can't find anything that does exactly what I need (some split at certain line numbers, some from the command line not a bash script, some before and after certain strings)... and I'm stuck. 我已经尝试调查awk和sed的相关文章,但是我发现这一切都如此令人困惑,因为我找不到能完全满足我需要的内容(有些是在某些行号处分割的,有些是从命令行而不是bash脚本,某些字符串之前和之后的字符串)...而我被卡住了。 As you can see, I can't even work out how to format this post properly (my head hurts so much)!
如您所见,我什至无法弄清楚如何正确设置帖子的格式(我的头好痛)!
Any help appreciated - thanks! 任何帮助表示赞赏-谢谢!
Looks like 看起来像
sed '/BLAH DE BLAH/Q'
would do the job in GNU sed. 将在GNU sed中完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.