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
# 8  
Old 04-03-2013
Please use code tags. Useless use of cat

I suppose totalcap.awk is giving you all the output to which you want to add the License data. If you can modify totalcap.awk, it would be easy to do it in there.

However,
Code:
awk -F";" '{val=data}NR==1{val=hdr}{$4=$4";"val}1' OFS=";" data="$lc" hdr="Licensed Capacity" capacity_report.csv > Capacity_report_licensedata.csv

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 9  
Old 04-04-2013
Thanks for your update. I will try that out tomorrow and let you know. Let me know if you want me to post the total cap.awk ? I can do that tomorrow.

---------- Post updated 04-04-13 at 08:32 AM ---------- Previous update was 04-03-13 at 11:10 PM ----------

Hi Ahamed,

I tried your command and it is prining the header properly at column 5 , but the data ( $lc) is printing on the column 1. Any idea ? Also the field separator is (,) as it's a CSV file.
# 10  
Old 04-04-2013
Try

Code:
paste -sd, temp_1 temp_2

This User Gave Thanks to pamu For This Post:
# 11  
Old 04-04-2013
Change -F; and OFS=; to -F, OFS=,
If still you have a problem, paste the output of header and data.

--ahamed
# 12  
Old 04-04-2013
Hi Ahamed,

This is the output I am getting.

Server Name, Total Capacity , Grid Utilization , Used capacity,Licensed Capacity
,31.00servername,7.8 TB,5.2%,406.9 GB

The value 31.00 should be for the last column ( also that should be separated by comma) but it's printing first name and the server name prints along with that without any comma & it omits some if it's name ( server name). updated code as per your suggestion.

Code:
 >capacity_report.csv
echo " Server Name, Total Capacity , Grid Utilization , Used capacity" >> capacity_report.csv
for i in ` cat serverist.txt ` ; do
command1 | awk -f totalcap.awk >> capacity_report.csv
lcapdata=`command 2 | 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
#awk 'NR==1{print $0, ",Licensed Capacity";next}{print $0, ",$lc TB"}' capacity_report.csv > Capacity_report_licensedata.csv

awk -F"," '{val=data}NR==1{val=hdr}{$4=$4","val}1' OFS="," data="$lc" hdr="Licensed Capacity" capacity_report.csv > Capacity_report_licensedata.csv

done
Thanks,

Ravi

Last edited by rrb2009; 04-04-2013 at 10:34 PM..
# 13  
Old 04-04-2013
Please use code tags as per forum rules.
You should've mentioned it was the last column rather than saying 5th column.

Try this
Code:
awk '{val=data}NR==1{val=hdr}{$0=$0","val}1' data="$lc" hdr="Licensed Capacity" capacity_report.csv > Capacity_report_licensedata.csv

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

Hi Ahmed - Still the output is same as the above I mentioned.






I am using the code tags for the codes but sometime it doesn't show up. I will verify before posting the code.

---------- Post updated at 03:19 PM ---------- Previous update was at 03:15 PM ----------

Pamu,

I tried your paste command but it merges both the files but all are printing in cloumn rather columns and it's respective row. This is how my 2 files are there.

tmp1_file.csv
***********
server name,total capacity,server utilization,used capacity
server1, 5 TB, 20%,1 TB

tmp2_file.csv
***********
Licensed Capacity
10.00 TB

I want to merge these 2 files to get an output like these.

server name,total capacity,server utilization,used capacity,Licensed Capacity
server1, 5 TB, 20%,1 TB,10.00 TB
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