Extract line beginning with a specific pattern in sed
Solution 1
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!
Solution 2
With sed
, to print specific lines, it's easier to use the -n
option and the p
command:
sed -rn '/IDno=|Type=Student/p'
Or:
sed -n -e '/IDno=/p' -e '/Type=Student/p'
The -n
option suppresses output unless explicitly print. The p
command, of course, prints matching lines.
Solution 3
awk
:Setting the field separator as
=
, and printing the records that containType=Student
as the whole record orIDno
as the first field:awk -F= '$1=="IDno" || $0=="Type=Student"'
perl
:Printing the lines that start with
IDno
followed by=
, or start withType
, followed by a=
and end inStudent
:perl -ne 'print if /^(IDno=|Type=Student$)/'
Example:
% cat file.txt
IDno="1"
Name=Jack
Type=Student
IDno="2"
Name=Jill
Type=Teacher
% awk -F= '$1=="IDno" || $0=="Type=Student"' file.txt
IDno="1"
Type=Student
IDno="2"
% perl -ne 'print if /^(IDno=|Type=Student$)/' file.txt
IDno="1"
Type=Student
IDno="2"
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
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?
-
Terrance about 7 yearsWouldn't
egrep
be easier?egrep -e "IDno=|Type=Student" inputfile
-
steeldriver about 7 yearsDid you mean to write
-e d
(not justd
)? otherwise the command is malformed I think. Regardless, it would be more idiomatic to invert the logic using!
rather than by branching past thed
i.e.sed '/IDno=\|Type=Student/!d'
-
-
heemayl about 7 years
egrep
is deprecated in favor ofgrep -E
, it's time to get rid of the cliché... -
Xen2050 about 7 yearsThis return every single "IDno=" line, regardless of whether or not the "Type=Student" two lines down. That appeared to be part of the original question, to only return IDno's for "Student"s, but the OP seems to like this, so...?
-
muru about 7 yearsThe example output clarifies that.
-
Xen2050 about 7 years@muru Thanks, I just noticed that too, seems to be a mistake IMO either in the description or example output. Actually found a mistake in my answer too, pasting the Name after ID... Maybe someone will find this answer useful anyway (especially after/if I fix it), I'm still learning from it\
-
Xen2050 about 7 yearsThat is good, pipeing it back to grep again to only return the ID & Type lines gets rid of the Name too (and the
--
separator line), like adding|grep "IDno=\|Type="
-
muru about 7 yearsread it this way "(all the IDno) and (the type only when type is student)"
-
Terrance about 7 years@heemayl Good point! =) Old habits die hard I guess. Maybe one day they'll remove the script, but from my understanding is that it is left in there for older applications that rely on them to not be modified.