Extract line beginning with a specific pattern in sed

8,330

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 contain Type=Student as the whole record or IDno as the first field:

    awk -F= '$1=="IDno" || $0=="Type=Student"'
    
  • perl:

    Printing the lines that start with IDno followed by =, or start with Type, followed by a = and end in Student:

    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"
Share:
8,330

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    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
      Terrance about 7 years
      Wouldn't egrep be easier? egrep -e "IDno=|Type=Student" inputfile
    • steeldriver
      steeldriver about 7 years
      Did you mean to write -e d (not just d)? otherwise the command is malformed I think. Regardless, it would be more idiomatic to invert the logic using ! rather than by branching past the d i.e. sed '/IDno=\|Type=Student/!d'
  • heemayl
    heemayl about 7 years
    egrep is deprecated in favor of grep -E, it's time to get rid of the cliché...
  • Xen2050
    Xen2050 about 7 years
    This 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
    muru about 7 years
    The example output clarifies that.
  • Xen2050
    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
    Xen2050 about 7 years
    That 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
    muru about 7 years
    read it this way "(all the IDno) and (the type only when type is student)"
  • Terrance
    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.