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
# 22  
Old 04-05-2013
Ahamed,

I tried just on the command line also , the result is still the same. BTW the OS is SuSE Enterprise Linux 11.

Do you know how can I merge ttwo csv files using awk ? I am planning to use that method as I mentioned previously. Anyhow Thanks for all your help.
# 23  
Old 04-05-2013
Merging is what we were doing. Not sure if I am overlooking something here.
Any ways, try this

Code:
awk 'NR==FNR{a[++x]=$0;next}FNR==1{print $0,","a[1];next}{print $0","a[2]}' tmp2_file.csv tmp1_file.csv

tmp2_file.csv is the file with License header and data. tmp1_file.csv is the one with server data.

If this also doesn't work, let's have a look at the totalcap.awk script.

--ahamed

---------- Post updated at 08:28 PM ---------- Previous update was at 08:23 PM ----------
Or try this
Code:
hdr="Licensed Capacity"
data="10.00 TB"
x=1
while read line
do 
  if [ $x -eq 1 ]; then   
    echo "$line,$hdr"
    ((x+=1))
  else   
    echo "$line,$data"
  fi
done < tmp1_file.csv

--ahamed
# 24  
Old 04-05-2013
The output for the both the commands are same what we were getting before. But in the first command I swapped the file names and it prints the licensed capacity and it's Data perfectly as 1st column.

Any clue that we were able to add as a 1st cloumn without any issues but we add in the last column it doesn't do that.

BTW here is the totalcap.awk script

cat totalcap.awk
Code:
# Field separator is two or more spaces
BEGIN { FS="   *";      OFS=",";        ORS="\r\n"      }
# Ignore first two lines, then store in array
NR>2 { sub(/ *$/, ""); A[$1]=$2; }

END {
        print A["System Name"],A["Total capacity"],A["Server utilization"],A["Capacity used"];}

# 25  
Old 04-05-2013
Can you try modifying ORS="\r\n" to ORS="\n" in the totalcap.awk script?

--ahamed

---------- Post updated at 09:09 PM ---------- Previous update was at 08:55 PM ----------

Make the following changes in your script
In your main script
Code:
echo " Server Name, Total Capacity , Grid Utilization , Used capacity, License Capacity" >> capacity_report.csv
...
command1 | awk -f totalcap.awk data="10.00 TB" >> capacity_report.csv

In totalcap.wak
Code:
...
print A["System Name"],A["Total capacity"],A["Server utilization"],A["Capacity used"],data}
...

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 26  
Old 04-05-2013
Thanks a lot Ahamed, finally it worked :-) ... Thanks a ton for all your help.

---------- Post updated at 09:53 AM ---------- Previous update was at 09:09 AM ----------

Ahamed,

I have one final question. How can I execute the whole script on a remote server via SSH ? Please advice if you know.

---------- Post updated at 11:19 AM ---------- Previous update was at 09:53 AM ----------

Hi Ahamed,

Never Mid, I found the solution , everything is working great as expected.

Thanks a lot again for all the help you did.

Regards,

Ravi
# 27  
Old 04-05-2013
Glad it worked!

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