提取以sed中特定模式开头的行

5 text command-line regex sed text-processing

我的输入文件是这样的:

IDno="1"
Name=Jack
Type=Student
IDno="2"
Name=Jill
Type=Teacher
Run Code Online (Sandbox Code Playgroud)

仅当类型为学生时,我才使用 sed 提取所有 IDno 和类型。

sed -e '/IDno=/b' -e '/Type=Student/b' d
Run Code Online (Sandbox Code Playgroud)

这让我得到了所有类型为 student 但不是 IDnos 的行。

我想得到

IDno="1"
Type=Student
IDno="2"
Run Code Online (Sandbox Code Playgroud)

但我得到

Type=Student
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Ter*_*nce 4

egrep可以从文件中获取多行。使用管道|作为分隔符,您可以根据需要提取任意多个不同的标准。 egrep相当于grep -E. egrep是在文件夹中找到的脚本/bin,其内容指向exec grep -E "$@".

例子:

egrep "IDno=|Type=Student" inputfile
Run Code Online (Sandbox Code Playgroud)

或者

grep -E "IDno=|Type=Student" inputfile
Run Code Online (Sandbox Code Playgroud)

应该输出:

IDno="1"
Type=Student
IDno="2"
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • `egrep` 已被弃用,取而代之的是 `grep -E`,是时候摆脱陈词滥调了...... (2认同)