Register
It is currently Mon Sep 22, 2014 4:10 am

adding a additional column in csv file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue May 31, 2011 9:01 pm   

Joined: Mon May 09, 2011 12:38 am
Posts: 2
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 report
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 attached below

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


You do not have the required permissions to view the files attached to this post.


Top
 Profile  
 PostPosted: Fri Jul 06, 2012 7:00 am   

Joined: Fri Jul 06, 2012 3:08 am
Posts: 4
Use awk's "-v" option to pass a variable from bash to awk...

Code:
#! /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 report
ps aux | awk '{$1=$1; print}' OFS=, | cut -d ',' -f1-4,10,11 | awk -v pc="$pc" 'BEGIN{FS=","} {if (NR == 1) print "PC," $2 "," $1 "," $5 "," $6 "," $3 "," $4 ; else print pc "," $2 "," $1 "," $5 "," $6 "," $3 "," $4} END{}' > "$pc _process.csv"


Top
 Profile  
 PostPosted: Fri Jul 06, 2012 6:45 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
hi,

Quote:
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"
this is silly.
simply:
Code:
ps aux | awk 'BEGIN{FS=OFS=","} $1=$1{print pc_name,$2,$1,$10,$11,$3,$4}' pc_name="$pc"  > "$pc_process.csv
and
Code:
netstat -pant | awk 'NR>2{$1=$1; print pc_name,$0}' OFS=',' pc_name="$pc" > "$pc _netstat.csv"


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP