Skip to main content

Awk if loop or not working [Resolved]

Am trying to parse the below source file. Am trying to extract the text between===== and ENDTIME. And am using the code in a seperate block below. But when i use || to filter multiple patterns its not working. However when i use single pattern in awk if block its able to filter. Can someone let me know whats wrong with the command, why the or block is not working?

Sourcefile:

STARTTIME --- 2019/09/19 12:00:44
==================================================================
Processname                                Node   Status   QueueAv
==================================================================
RtpNm01                                   CE_01      run      full
RtpAcp01                                  CE_01      run      full
RtpDynManLite01                           CE_01      run      full
RtpDynLiteInc01                           CE_01      run      full
ENDTIME --- 2019/09/19 12:00:45
Expected Result:
RtpNm01                                   CE_01      run      full
RtpAcp01                                  CE_01      run      full
RtpDynManLite01                           CE_01      run      full
RtpDynLiteInc01                           CE_01      run      full

Code:

cat rtpServiceStatusrun | awk '/Processname\s*Node\s*Status\s*QueueAv/{
while (getline){
        if( $0 !~ /^====*/ || $0 !~ /^ENDTIME*/ ){
                        print $0
                }
        }
}'

Question Credit: Sathish Prakasam
Question Reference
Asked September 21, 2019
Tags: awk
Posted Under: Unix Linux
21 views
3 Answers

There are many ways to do this, including:

$ awk '/^ENDTIME/ {next}; NR>4' sourcefile 
RtpNm01                                   CE_01      run      full
RtpAcp01                                  CE_01      run      full
RtpDynManLite01                           CE_01      run      full
RtpDynLiteInc01                           CE_01      run      full

This prints all input lines after the fourth line, except for any line beginning with ENDTIME.

I think this is what you were trying to do with that weird while (getline) loop in the /Processname\s*....../ block - there was no need to do that, awk already loops over every line of input, and you can test the value of NR (current input line number).

and:

awk '! /^(STARTTIME|ENDTIME|=====|Processname)/' sourcefile

grep -vE '^(STARTTIME|ENDTIME|=====|Processname)' sourcefile

sed -E -e '/^(STARTTIME|ENDTIME|=====|Processname)/d' sourcefile

All of these print any lines not matching the regexp. They all use the same extended regular expression (ERE - default with awk, enabled with -E for both grep and sed), to look for any of the alternations at the beginning of the line (anchored with the ^).

and:

sed -e '1,4d; $d' sourcefile

Delete the 1st to 4th lines and the last line.


credit: cas
Answered September 21, 2019

One option:

cat file | head -n -1 | tail -n +5

credit: guillermo chamorro
Answered September 21, 2019

I changed or (||) to add (&&) and it started working.

cat rtpServiceStatusrun | awk '/Processname\s*Node\s*Status\s*QueueAv/{
while (getline){
        if( $0 !~ /^====*/ && $0 !~ /^ENDTIME*/ ){
                        print $0
                }
        }
}'

credit: Sathish Prakasam
Answered September 21, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA