简体   繁体   English

Bash 打印除开始到结束模式之外的所有行

[英]Bash print all line except start to end pattern

I have this input file with list of player names.我有这个带有玩家姓名列表的输入文件。

Cristiano Ronaldo
Anthony Martial
Jadon Sancho
Marcus Rashford
Bruno Fernandes
Fred
Scott McTominay
Donny Van De Beek
# Start U23 Team
Alvaro Fernandez
Zidane Iqbal
Facundo Pellistri
Hannibal Mejbri
Charlie Savage
# End U23 Team
Harry Maguire
Lisandro Martinez
Alex Telles

I want to exclude line starting with Start U23 Team and end with End U23 Team , so the end result will be:我想排除以Start U23 Team开头并以End U23 Team结尾的行,因此最终结果将是:

Cristiano Ronaldo
Anthony Martial
Jadon Sancho
Marcus Rashford
Bruno Fernandes
Fred
Scott McTominay
Donny Van De Beek
Harry Maguire
Lisandro Martinez
Alex Telles

I can print the U23 Player with this command awk "/# End U23 Team/{f=0} f; /# Start U23 Team/{f=1}" file , but how to do vice versa ?我可以使用这个命令打印 U23 播放器awk "/# End U23 Team/{f=0} f; /# Start U23 Team/{f=1}" file ,但是反之如何呢?

使用sed中的d命令排除行。

sed '/# Start U23 Team/,/# End U23 Team/d' file
awk "/# Start U23 Team/ {f=1} !f {print} /# End U23 Team/ {f=0}" file
awk '/# Start U23 Team/,/# End U23 Team/{next}1' file

Using sed使用sed

$ sed -z 's/# Start.*# End U23 Team\n//' input_file
Cristiano Ronaldo
Anthony Martial
Jadon Sancho
Marcus Rashford
Bruno Fernandes
Fred
Scott McTominay
Donny Van De Beek
Harry Maguire
Lisandro Martinez
Alex Telles

With GNU awk , please try following code, written and tested with shown samples only.使用 GNU awk ,请尝试以下代码,仅使用所示示例编写和测试。 Simply making use of RS variable where setting it to regex '(^|\n)# Start U23 Team\n.*\n# End U23 Team\n as per requirement and then in main program simply printing the lines.只需使用RS变量将其设置为正则表达式'(^|\n)# Start U23 Team\n.*\n# End U23 Team\n根据要求,然后在主程序中简单地打印行。

awk -v RS='(^|\n)# Start U23 Team\n.*\n# End U23 Team\n' '{sub(/\n$/,"")} 1' Input_file

I can print the U23 Player with this command awk "/# End U23 Team/{f=0} f; /# Start U23 Team/{f=1}" file , but how to do vice versa ?我可以使用这个命令打印 U23 播放器awk "/# End U23 Team/{f=0} f; /# Start U23 Team/{f=1}" file ,但是反之如何呢?

This code might be reworked following way此代码可能会按照以下方式进行返工

awk 'BEGIN{f=1} /# Start U23 Team/{f=0} f; /# End U23 Team/{f=1}' file

Explanation: BEGIN pattern allows executing action before starting processing lines, here I set f value to 1 , /# Start U23 Team/ was swapped with /# End U23 Team/说明: BEGIN模式允许在开始处理行之前执行操作,这里我将f值设置为1/# Start U23 Team//# End U23 Team/交换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM