Logo Background

AWK Find Match Line Command

  • By on September 18, 2009 | No Comments

    If you have a file that you want to search through the lines for match expression, awk line command will be fit for your case. It would be easy to use grep to search for the strings that match the expression, but awk has some more powerful feature to offer.

    Let’s say you want to seearch find 2 lines which was in sequence in the file containing the letter (case sensitive) as below:

    Unix
    Solaris

    It might be hard to search such sequence using the grep command alone. But you can use awk to search for “Unix” and then print the next line, which might be the Solaris.

    There is no one line command to achieve this requirement, so you have to write a Shell Script to process the line capture in the awk command.

    Please refer below for the simple Shell Script created for AWK Find Match Line Command.

    #Read matching pattern and get the next line
    output=`awk '/Unix/{getline;print}' $FILENAME.tmp`
     
    if [ output -eq Solaris ]; then
         echo  "You are using Sun Solaris OS"
       else
         echo  "You are using Linux OS maybe"
    fi
    Previous
    Next
    » OIDSRV Failover Script Check
Leave a Comment