简体   繁体   English

如何在Bash脚本中包含定义的字符串的行之后(包括该行)删除文件中的所有行?

[英]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.

相关问题 如何从匹配行之后删除文件中的所有行? - How do I delete all lines in a file starting from after a matching line? 如何将 bash 脚本的输出写入包含标题的 CSV 文件的第二行? - How do I write an output from bash script to the second line of the CSV file that contains headers? 如何将给定的bash脚本的所有命令行参数(包括字符串参数)原样传递给子进程? - How do I pass all command line arguments given to a bash script including string parameters as-is to a child process? 如何用另一个文件中的内容替换一个文件中第XX行之后的所有行? - How do I replace all lines after line XX in one file with content from another file? 在 Bash 中,如何在文件的每一行之后添加一个字符串? - In Bash, how do I add a string after each line in a file? 如何从文件中删除与定义变量中的字符串完全匹配的行? 猛击 - How to delete a line from a file that matches exactly to a string from a defined variable? bash 如何在多行中分隔一行Bash脚本? - How Do I Separate a Line of Bash Script Over Multiple Lines? 如何在使用 csv 脚本处理后删除 csv 文件中的一行 bash 脚本 - How to delete a line in a csv file after processing it with a Linux bash script 如何从bash脚本中删除root拥有的文件? - How do I delete a file owned by root from within bash script? 如何编写 bash 脚本以按照给定模式修改目录中的所有文件名? - How do I write a bash script to modify all file names in a directory after a given pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM