Ubuntu – Extract line beginning with a specific pattern in sed

command lineregexsedtext processingtext;

My input file is like this:

IDno="1"
Name=Jack
Type=Student
IDno="2"
Name=Jill
Type=Teacher

I am using sed to extract all the IDno and the type only when type is student.

sed -e '/IDno=/b' -e '/Type=Student/b' d

This gets me all lines with type student but not the IDnos.

I want to get

IDno="1"
Type=Student
IDno="2"

but I am getting

Type=Student

What am I doing wrong?

Best Answer

egrep can get multiple lines from a file. Using a pipe | as a separator you can pull as many different criteria as you want. egrep is the equivalent of grep -E. egrep is a script found in the /bin folder with the contents pointing to exec grep -E "$@".

Example:

egrep "IDno=|Type=Student" inputfile

or

grep -E "IDno=|Type=Student" inputfile

Should output:

IDno="1"
Type=Student
IDno="2"

Hope this helps!