add a additional column in csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add a additional column in csv file
# 1  
Old 06-01-2011
add a additional column in csv file

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
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 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 provided as a attachment

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

Last edited by Franklin52; 06-01-2011 at 06:32 AM.. Reason: Please use code tags
# 2  
Old 06-01-2011
Hi, try:
Code:
awk '{$1=pc OFS $1; print}' OFS=, pc=$pc

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add current time stamp column in existing csv file

Hi , I want to add a new column 'current_time stamp' in my existing csv file with current time stamp for all the records.I tried something this but this is printing 0 with date & time and printed date one line above header.Please help awk -F "," 'BEGIN{ OFS="," } {$6=system("date... (5 Replies)
Discussion started by: netdbaind
5 Replies

2. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

3. Shell Programming and Scripting

awk to extract multiple values from file and add two additional fields

In the attached file I am trying to use awk to extract multiple values and create the tab-delimited desired output. In the output R_Index is a the sequential # and Pre_Enrichment is defaulted to .. I can extract from the values to the side of the keywords, but most are above and I can not... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

5. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

6. Shell Programming and Scripting

How to add a new column in a csv file?

Hi All, I would like to add a new column in my input.csv file as well as also want to add a new page in same .csv file. Plz help me any help should highly be appreciated...:) (8 Replies)
Discussion started by: Ashish Singhal
8 Replies

7. Shell Programming and Scripting

Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has... (2 Replies)
Discussion started by: Ganesh L
2 Replies

8. Shell Programming and Scripting

Inserting additional comma delimiters in a csv file, after and before certian fields.

Hello I have a csv file which I need to insert addtional commas into. The csv is of the format field1,field2,field3,field4,...etc...,field13,field14 I need to add extra commas in each record so that the final output looks like ... (1 Reply)
Discussion started by: kamal_p_99
1 Replies

9. Shell Programming and Scripting

Add additional numbers to file

I need to change the following field from: "7/3/2009 7:07:12 PM","12345676","ok","8674" "6/3/2009 8:07:12 PM","12345676","ok","8674" "5/1/2009 7:07:12 PM","12345676","ok","8674" "4/9/2009 3:07:12 AM","12345676","ok","8674" "3/8/2009 3:07:12 PM","12345676","ok","8674" "2/7/2009 4:07:12... (10 Replies)
Discussion started by: Pablo_beezo
10 Replies
Login or Register to Ask a Question