如何在bash中仅提取两个字符串之间多行的第一个实例?

Vyo*_*eya 3 shell awk sed

我的文件是:

abc
123
xyz
abc
675
xyz
Run Code Online (Sandbox Code Playgroud)

我想提取:

abc
123
xyz
Run Code Online (Sandbox Code Playgroud)

(123 可以是任何东西,重点是我想要第一次出现)

我尝试使用这个:

sed -n '/abc/,/xyz/p' filename
Run Code Online (Sandbox Code Playgroud)

但这给了我所有的实例。我怎么能得到第一个?

Rav*_*h13 5

您能否尝试使用所示示例进行以下,编写和测试。

awk '/abc/{found=1} found; /xyz/ && found{exit}'  Input_file
Run Code Online (Sandbox Code Playgroud)

或者根据 Ed Sir 的评论,为了提高效率,请尝试以下操作。

awk '/abc/{found=1} found{print; if (/xyz/) exit}'  Input_file
Run Code Online (Sandbox Code Playgroud)

说明:为以上添加详细说明。

awk '               ##Starting awk program from here.
/abc/{              ##checking condition if a line has abc in it then do following.
  found=1           ##Setting found here.
}
found;              ##Checking condition if found is SET then print that line.
/xyz/ && found{     ##Checking if xyz found in line and found is SET then do following.
  exit              ##exit program from here.
}
'  Input_file       ##Mentioning Input_file name here.
Run Code Online (Sandbox Code Playgroud)