Csv format output file using scirpt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Csv format output file using scirpt
# 1  
Old 04-10-2013
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.

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

my input file look like this :
testreport.txt

Code:
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)

Code:
Tabular format output should look like :
|============================================|
|   Report Status                                                          |
|============================================|
|       Server   Software Checks                                      |
|============================================|
| ServerA     passed                                                     |
| ServerB     passed                                                      |
| ServerC     passed                                                      |
| ServerD     passed                                                       |
| ServerE     failed                                                        |
| ServerH     passed                                                     |
|============================================|


Code:
csv format output file :
Server,Software Checks
ServerA,passed
ServerB,passed
ServerC,passed
ServerD,passed
ServerE,passed
ServerH,passed

# 2  
Old 04-10-2013
Here is an awk program:
Code:
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:
Code:
|===============================================|
|       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.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-10-2013
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
# 4  
Old 04-10-2013
Quote:
Originally Posted by ajayram_arya
For CSV report you can use

bash-3.00$ cat testtxt | awk '{print $1" "$3}' | sed 's/ /,/g'
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 ","
Code:
awk ' { print $1, $3 } ' OFS="," filename

This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-10-2013
Code:
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[i],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
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 04-11-2013
thanks Yoda. your script worked very well.

thanks Again.
# 7  
Old 04-11-2013
Thank you Yoda for explaining in detail ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

How to translate df -h output into a CSV format?

Hi, Can someone please let me know as how I can send the below df -h format of a linux system into a CSV format ? Resource Size GiB Used GiB Avail GiB Use% Cleanable GiB* ---------------- -------- -------- --------- ---- -------------- /data: pre-comp -... (10 Replies)
Discussion started by: new2prog
10 Replies

3. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

4. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

5. Shell Programming and Scripting

Save output of updated csv file as csv file itself

Hi, all I want to sort a csv file based on timestamp from oldest to newest and save the output as csv file itself. Here is an example of my csv file. test.csv SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0739.JPG,2015:02:17 11:32:21 /home/intannf/foto/IMG_0749.JPG,2015:02:17 11:37:28... (10 Replies)
Discussion started by: refrain
10 Replies

6. Shell Programming and Scripting

UNIX command output in csv format

I'm just wondering is there any way to capture the output of a unix command in a csv format. df -h gives the result of filesystem,free space,Used space, use %,mounted on. Is there a way to capture the command output and format it as comma sparated or fixed length file. (3 Replies)
Discussion started by: anita81
3 Replies

7. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

8. Shell Programming and Scripting

formatting into CSV format of SQL session output

I am getting a no of fields from a SQL session (e.g. select a,b,c from table). How do I convert the output values into CSV format . The output should be like this 'a','b','c', (4 Replies)
Discussion started by: mady135
4 Replies

9. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

10. Shell Programming and Scripting

format output in csv file

I am sending the output of a file to .csv file. The output should look like this: Total Customers Processed:,8 Total Customers Skipped:,0 Total Customers Added:,8 Total Customers Changed:,0 Total Policies Deleted:,0 Total Policies Failed:,0 total:,8 Now i want this output in... (1 Reply)
Discussion started by: Prashant Jain
1 Replies
Login or Register to Ask a Question