Csv format output file using scirpt

Hi All,

I get the test result file daily after running the main test script. from the resultfile, need to fetch only server info and status and put them in tabular format in a file and as well in CSV format output file.

I tried using awk command but am not able to put them in tabluar format as well in csv foramt output file.

basically, single script should produce both output file.

awk '{ print $1 $5 }' testreport.txt

my input file look like this :
testreport.txt

Starting collect reports 
CollectReport
Not receive test report from ServerAA
Not receive test report from ServerF
Not receive test report from ServerG
Not receive test report from ServerJ
Not receive test report from ServerK
 
Testing Date :  04/09/13 12:26:22
Checking Result - Software Checks
ServerA: Software checks is passed. (Latest version : DT 2.1 for DT/AT to support XGen:  DT 2.1)
ServerB: Software checks is passed. (Latest version : Synx : CONFIG_Sol-2013-SI-C_upgrade_01 package upgrade for SSI)
ServerC: Software checks is passed. (Latest version : DT 2.1 for DT/AT to support XGen:  DT 2.1)
ServerD: Software checks is passed. (Latest version : Synx : CONFIG_Sol-2012-SI-B_upgrade_02 package upgrade for SSI)
ServerE: Software checks is failed. (Latest version : EET : CONFIG_Sol-2011-SI-C_upgrade_01 package upgrade for EET)
ServerH: Software checks is passed. (Latest version : ASI : CONFIG_Sol-2012-SI-C_upgrade_01 package upgrade for ASI)
Tabular format output should look like :
|============================================|
|   Report Status                                                          |
|============================================|
|       Server   Software Checks                                      |
|============================================|
| ServerA     passed                                                     |
| ServerB     passed                                                      |
| ServerC     passed                                                      |
| ServerD     passed                                                       |
| ServerE     failed                                                        |
| ServerH     passed                                                     |
|============================================|
csv format output file :
Server,Software Checks
ServerA,passed
ServerB,passed
ServerC,passed
ServerD,passed
ServerE,passed
ServerH,passed

Here is an awk program:

awk '
        BEGIN {
                print "|===============================================|"
                print "|        R E P O R T   S T A T U S              |"
                print "|===============================================|"
                print "| Server            Software Checks             |"
                print "|===============================================|"
        }
        /^Server/ {
                sub(/:/, x, $1)
                sub(/\./, x, $5)
                print "| " $1 "\t\t" $5 ,"\t\t\t|"
        }
        END {
                print "|===============================================|"
} ' testreport.txt

Output:

|===============================================|
|       R E P O R T   S T A T U S               |
|===============================================|
| Server            Software Checks             |
|===============================================|
| ServerA               passed                  |
| ServerB               passed                  |
| ServerC               passed                  |
| ServerD               passed                  |
| ServerE               failed                  |
| ServerH               passed                  |
|===============================================|

Modify above code to create CSV output.

1 Like

For CSV report you can use

bash-3.00$ cat testtxt | awk '{print $1" "$3}' | sed 's/ /,/g'
san,
sam,
cat,
cat,act
cat,mat

You got yourself a Useless Use of Cat Award

Please note that the purpose of cat is to concatenate file, there is no need to cat a file and pipe the output to awk program.

Remember awk is capable of opening and reading a file by itself, thus you can get rid of a pipeline that costs you a process.

Also sed which costs you another process is unnecessary because you can use awk OFS (output field separator) to set the field separator to comma ","

awk ' { print $1, $3 } ' OFS="," filename
1 Like
awk 'BEGIN{a="|";b="="}
function prt_hdr_ftr(val){
  printf a; for(i=0;i<=50;i++)printf b;print a
}
function prt_data(){
 for(i in data) printf "%-s %-25s%-10s%16s\n",a,i,data,a
}
/Checking Result/{split($0,x,"-");c=x[2];p=1;next}
p{split($0,x,"[: .]");data[x[1]]=x[6]}
END{
prt_hdr_ftr();printf "%-s%20s%32s\n",a,"Report Status",a
prt_hdr_ftr();printf "%-s %-25s%-10s%10s\n",a,"Server",c,a
prt_hdr_ftr()prt_data()prt_hdr_ftr()
}'  infile

--ahamed

1 Like

thanks Yoda. your script worked very well.

thanks Again.

Thank you Yoda for explaining in detail ...