Adding a new column to a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a new column to a csv file
# 1  
Old 04-03-2013
Adding a new column to a csv file

Hi,

Can anyone please explain me how can I add a new column to a Csv file ?

Actaully I am setting one set of commands and creating a CSV file with 4 columns.

I am executing another command and it's output should be added as column 5 with a column heading ?

Can anyone explain how can I do this with awk or sed and explain me ?

Thanks in advance for the help.
# 2  
Old 04-03-2013
Well, we need to know how you have put up the script/commands so that we can suggest a better solution.
Does the so called "other command" provide a single output? Or does it provide different output for different rows in the 5th column?

--ahamed
# 3  
Old 04-03-2013
Hi,

Thanks for your quick reply. Yes the second command gives me just one output.

For example from the first output I will get few fields like this.

Server Name, Total Capacity, Utilization (%), used Capcity
Data 1, Data2, Data3, Data4,

I will be executing another command which should another colum as Licensed and the Data will be produed from the output.

.
# 4  
Old 04-03-2013
so, the first output i.e. Server Name etc, where are you storing those values? into a file/variable? is it in a loop?

--ahamed
# 5  
Old 04-03-2013
Yes , it's in a loop where it will read the server names that the commands should be executed on.

I also thought of doing in another way by creating two CSV files ( 1st one for the 1st output & 2nd one for the 2nd output ) and to merge them. But simple Cat and join are not working. It's not merging both the files and giving me a single file.

Any thoughts ?
# 6  
Old 04-03-2013
Can you paste the relevant part of the script where you are doing this operation i.e. getting the Server Name, Total Capacity etc

--ahamed
# 7  
Old 04-03-2013
Here it is, This one works but adds the column from output as column 1. I want that as column 5.

Code:
for i in ` cat serverlist.txt ` ; do
command1 | awk -f totalcap.awk >> capacity_report.csv
lcapdata=`command2 | grep protected-data-max | cut -d\" -f2`
lc=`echo "scale=2 ; $lcapdata/1024" | bc `
#echo $lc >> tmp_file.csv
awk -vc="Licensed Capacity" -vd="$lc TB"  'NR==1{$0=c","$0}NR!=1{$0=d","$0}1' capacity_report.csv  > Capacity_report_licensedata.csv
done


My 2nd script ( This one also works but I don't know how to merge the tmp1 and tmp2 files).

Code:
#!/bin/bash
>tmp1_file.csv
>tmp2_file.csv
echo " Server Name, Total Capacity , Server Utilization , Used capacity" >> tmp1_file.csv
echo "Licensed Capacity" >> tmp2_file.csv
for i in ` cat gridlist.txt `; do
ssh admin@$i command 1 | awk -f totalcap.awk >> tmp1_file.csv ; command 2 | grep protected | awk -F\" '{print $2 / 1024}' | bc -l | xargs printf  '%.2f \n' >> tmp2_file.csv  ; done


Second one is the one I am thinking to do because I need use ssh into all the servers. In the 1st script I did not do that.

If you could guide me that would be great.

Last edited by Franklin52; 04-04-2013 at 03:19 AM.. Reason: fixed code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding new column data in csv from UNIX

Adding new column data in csv from UNIX Hi I need to add new column data daily to existing csv file. Please assist 7/11 7/10 7/9 7/8 space 10 GB 20 GB I was able to generate current day's data in csv but unable to add the previous 30 days data to the same csv Please use code tags,... (2 Replies)
Discussion started by: archana25
2 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

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

4. 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

5. 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

6. Shell Programming and Scripting

Adding Extra Commas to a CSV file

Trying in this forum. Not sure if it is permitted.... but in need of help. Please find the requirements in the below link. https://www.unix.com/unix-dummies-questions-answers/191503-add-extra-commas-csv-file-2.html#post302665179 Thanks in Advance. (1 Reply)
Discussion started by: chillblue
1 Replies

7. Shell Programming and Scripting

adding quotes around each column in a csv file

I saved the csv file in a comma delimited format. Sample input input.csv 1 abc 2 2 def 4 3 ghi 6 4 jkl 8 5 mno 10 output.csv should look like this with single quotes around each field '1' 'abc' '2' '2' 'def' '4' '3' 'ghi' '6' '4' 'jkl' '8' '5' 'mno' '10' Please help me :confused:... (3 Replies)
Discussion started by: melannie
3 Replies

8. UNIX for Advanced & Expert Users

Help in adding in csv file

Hi experts I have a .csv file which contains the csv is comma seperated file col1 col2 1 2 3 4 5 6 7 8 I want the colums to be added and the output must be col1 col2 col3 1 2 3 3 4 7 5 6 11 7 8 15 Please help:( (1 Reply)
Discussion started by: mariselvam
1 Replies
Login or Register to Ask a Question