0

For my CGI in bash/html, I have this script ( juste the awk script, the rest of the code is just a simple hmtl code ) :

for fn in /var/www/cgi-bin/LPAR_MAP/*; do
    awk -F',|;' 'NR==1 { split(FILENAME ,a,"[-.]");
       print "DATE ========================== : " a[4] }
       /'$test'/ { 
           print ""
           print "LPARS :" $2
           print "RAM : " $5
           print "CPU 1 : " $6
           print "CPU 2 : " $7
           print "" 
           print ""}' $fn;
done

My script analyze many CSV files and display the informations that I need like that :

DATE ========================== : 20180122

LPARS :miaibye01
RAM : 60
CPU 1 : 1.0
CPU 2 : 2



LPARS :miaibg04
RAM : 99
CPU 1 : 1.5
CPU 2 : 3



LPARS :miaibk07
RAM : 52
CPU 1 : 2.5
CPU 2 : 5

DATE ========================== : 20180124

LPARS :miaibye01
RAM : 60
CPU 1 : 1.0
CPU 2 : 2



LPARS :miaibg04
RAM : 99
CPU 1 : 1.5
CPU 2 : 3



LPARS :miaibk07
RAM : 52
CPU 1 : 2.5
CPU 2 : 5



LPARS :miaibv176
RAM : 0.25
CPU 1 : 0.1
CPU 2 : 1

...

But sometimes, When my script doesn't find the informations asked, it display the informations asked and juste the date of the file in which the script search the informations. The output is :

DATE ========================== : 20180923
DATE ========================== : 20180924
DATE ========================== : 20180925
DATE ========================== : 20180926
DATE ========================== : 20180927
DATE ========================== : 20180928
DATE ========================== : 20180929
DATE ========================== : 20180930
DATE ========================== : 20181001

I searched how to delete ( or not to show ) these useless line with my awk script, but I don't know how... Do you have any idea to do this ?

2 Answers 2

1

you can simply do this :

for fn in /var/www/cgi-bin/LPAR_MAP/*; do
    awk -F',|;' -v test="$test" '
       NR==1 { 
         split(FILENAME ,a,"[-.]");
       }
       $0 ~ test {
           if(!header++){
               print "DATE ========================== : " a[4] 
           }
           print ""
           print "LPARS :" $2
           print "RAM : " $5
           print "CPU 1 : " $6
           print "CPU 2 : " $7
           print "" 
           print ""
       }' $fn;
done

I print the header only the first time when $0 ~ $test. Note that I have change a little bit your code to create an awk variable equal to $test. It will be more robust to code injection.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @EdMorton !
Hello ! Your solution works great but as you can see here ( image.noelshack.com/fichiers/2019/30/5/1564126963-nbvc.png ), even if the useless line are not displayed, the table still generated ! Do you have any idea to solve that ?
I don't have any context on that image ... Where do you get that from ?
Hello there ! With your script, it's possible not to display the lines " Date ====== XXX " if my script can't find informations. But, even if there is no informations, the html columns which was supposed to receive the informations are still generated and displayed ( that are the empty columns in the right on the pictures )
I have no idea how you generate these html columns. I think that we should see the rest of the code and it may be more readable if you add an other question on so with appropriate tags.
1

You don't need a shell loop for this, all you need is:

awk -F'[,;]' -v test="$test" '
    FNR==1 { doneHdr = 0 }
    $0 ~ test {
        if ( !doneHdr++ ) {
            split(FILENAME,a,/[-.]/)
            print "DATE ========================== : " a[4]
        }
        print ""
        print "LPARS :" $2
        print "RAM : " $5
        print "CPU 1 : " $6
        print "CPU 2 : " $7
        print "" 
        print ""
    }
' /var/www/cgi-bin/LPAR_MAP/*

2 Comments

Hello ! Thank you for your help ! Very helpul !! Can you explain me quickly the functioning of these lines :" test="$test" ' FNR==1 { doneHdr = 0 } $0 ~ test { if ( !doneHdr++ ) " ? Thank you a lot !
-v test="$test" is setting an awk variable named test to the velue of your existing shell variable also named test - see cfajohnson.com/shell/cus-faq-2.html#Q24. $0 ~ test is just corrected syntax for your existing /'$test'/. ` ' FNR==1 { doneHdr = 0 }` is clearing a flag at the start of reading every new input file. if ( !doneHdr++ ) x will execute x and set the flag to 1 the first time it's hit, after that x won't be executed again while the flag is non-zero (i.e. for the rest of the current input file).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.