bash script to add additional column in csv

Red hat Redhat jboss monitoring f/soa 4c...
June 1, 2011 at 02:32:48
Specs: windows, 256
Thanks for allwoing me to discuss in this forum

GIVEN BELOW A simple shell script which will ask for the user to input a PC name and it will produce the output in CSV with the PC name


#! /bin/bash
read -p "enter the PC name :" pc
#checking for netstat report
netstat -pant |sed '1,2d' |awk '{$1=$1; print}' OFS=, > "$pc _netstat.csv"

#checking for process list
ps aux | awk '{$1=$1; print}' OFS=, | cut -d ',' -f1-4,10,11 | awk ' BEGIN{FS=","} {print $2 "," $1 "," $5 "," $6 ", " $3 "," $4} END{}' > "$pc _process.csv"


The output is as follows

process.csv
1,root,0:01,init,0,0
2,root,0:00,[kthreadd],0,0
3,root,0:00,[migration/0],0,0
4,root,0:00,[ksoftirqd/0],0,0
5,root,0:00,[migration/1],0,0

netstat.csv
tcp,0,0,127.0.0.1:3310,0.0.0.0:*,LISTEN,3203/clamd
tcp,0,0,127.0.0.1:631,0.0.0.0:*,LISTEN,3148/cupsd
tcp,0,0,192.168.2.101:56014,66.220.153.29:80,TIME_WAIT,-

Now how can i make changes in script so that i should get a additional column in the beginning of csv file with PC name in each field....

suppose if Lenovo_pc is the PC Name which user entered then output should be as follows--
netstat_csv file


Lenovo_pc,tcp,0,0,127.0.0.1:3310,0.0.0.0:*,LISTEN,3203/clamd
Lenovo_pc,tcp,0,0,127.0.0.1:631,0.0.0.0:*,LISTEN,3148/cupsd
Lenovo_pc,tcp,0,0,192.168.2.101:56014,66.220.153.29:80,TIME_WAIT,-


process_csv file

Lenovo_pc,1,root,0:01,init,0,0
Lenovo_pc,2,root,0:00,[kthreadd],0,0
Lenovo_pc,3,root,0:00,[migration/0],0,0
Lenovo_pc,4,root,0:00,[ksoftirqd/0],0,0
Lenovo_pc,5,root,0:00,[migration/1],0,0
Lenovo_pc,6,root,0:00,[ksoftirqd/1],0,0
Lenovo_pc,7,root,0:00,[events/0],0,0

any suggestions in this regard is greatly appreciated
thanks in advance



See More: bash script to add additional column in csv

Report •

#1
June 1, 2011 at 07:00:04
How about embedding the $pc variable in the awk script?

# UNTESTED
awk ' BEGIN{FS=","} {print "'"$pc"'" "," $2 "," $1 "," $5 "," $6 ", " $3 "," $4} END{}' > "$pc _process.csv"

BTW, there are multiple ways to pass a shell variable to awk. Check out this link:

http://www.tek-tips.com/faqs.cfm?fi...


Report •
Related Solutions


Ask Question