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
# 15  
Old 04-04-2013
Works for me though
Code:
[root@Imperfecto_1 ~]# cat input_file
server name,total capacity,server utilization,used capacity
server1, 5 TB, 20%,1 TB

[root@Imperfecto_1 ~]# awk 'NR==1{$0= $0 OFS hdr;print;next}{$0 =$0 OFS data}1' hdr="Licensed Capacity" data="10.00 TB" OFS=, input_file
server name,total capacity,server utilization,used capacity,Licensed Capacity
server1, 5 TB, 20%,1 TB,10.00 TB

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 16  
Old 04-04-2013
Hi Ahamed,

I just tried your command , without my data variable but still the output is same. My server name is FQDN ( i.e Servername.domainname.com) in the first column. Does that creating any issues. I do not know why the data value is printing first and the column header is printing properly.

Last edited by rrb2009; 04-04-2013 at 05:25 PM.. Reason: typo error
# 17  
Old 04-04-2013
Can you paste the output of your script with -x option? i.e. bash -x <your script>

--ahamed
# 18  
Old 04-04-2013
Hi Ahmed,

Here it is.

Code:
sh -x capacity_report.sh
+ echo ' Server Name, Total Capacity , Server Utilization , Used capacity'
++ cat serverlist.txt
+ for i in '` cat gridlist.txt `'
+ command1
+ awk -f totalcap.awk
++ command2
++ grep protected-data-max
++ cut '-d"' -f2
+ lcapdata=31744
++ echo 'scale=2 ; 31744/1024'
++ bc
+ lc=31.00
+ awk 'NR==1{$0= $0 OFS hdr;print;next}{$0 =$0 OFS data}1' 'hdr=Licensed Capacity' 'data=10.00 TB' OFS=, capacity_report.csv
 Server Name, Total Capacity , Server Utilization , Used capacity,Licensed Capacity
,10.00 TBtil.att.com,7.8 TB,5.2%,406.9


Last edited by rrb2009; 04-04-2013 at 10:34 PM..
# 19  
Old 04-04-2013
what is the content of the file capacity_report.csv which gets generated?

--ahamed
# 20  
Old 04-04-2013
Server Name, Total Capacity , Server Utilization , Used capacity
servername.domain.name.com,7.8 TB,5.2%,406.9 GB
# 21  
Old 04-05-2013
That is so weird! Which is your OS? And did you try running that awk command alone with the input file?

--ahamed
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