Reg - Working on a CSV File in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reg - Working on a CSV File in a script
# 8  
Old 04-22-2013
Hi,

Thanks for the replies. I will be more specific.

The 1st CSV file contains 3 columns ( Client Name, Domain and Server Name ).

It will have more than 2000+ entries.

The 2nd CSV file contains 2 columns ( Server Name, Value ).

This will have just 30 to 40 entries for all serves and value belongs to each server. The common value on both the CSV file is the server name.

I want to append the value from 2nd CSV file to the 1st one based on the sever name. As I have little knowledge on awk / sed , I do not know if that can be achieved.

Just want to check here how would I able to do this ?

Thanks and let me know if I need to give an other specifics.

Regards,

Ravi
# 9  
Old 04-22-2013
Quote:
Originally Posted by rrb2009
Hi,

Thanks for the replies. I will be more specific.
[..]
Thanks and let me know if I need to give an other specifics.
What did not work? What is your OS and version? Smilie


Also, what field separator do your CSV's use?
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 04-22-2013
It would help to post meaningful test input case, and the expected output. And of course use those code tags.
This User Gave Thanks to hanson44 For This Post:
# 11  
Old 04-22-2013
Hi Scrutinizer,

I just tried your command , but it did not give me any results and it just returned to the prompt. The field separator is "," and the OS is - SuSe Ent Linux 11.

Hi hanson44,

I think I posted the files in my original post and I am posting it again for your reference.

1st CSV File
Code:
ColumnA,ColumnB,ColumnC  
Client 1,XXXXX,Server A 
Client 2,XXXXX,Server B  
Client 3,XXXXX,Server C
Client 4,XXXXX,Server C
Client 5,XXXXX,Server B
Client 6,XXXXX,Server C

The 1st CSV file contains 3 columns ( Client Name, Domain and Server Name ).
It will have more than 2000+ entries.

2nd CSV file

Code:
ColumnA,CloumnB    
Server A,value 1 
Server B,value 2  
Server C,value 1

The 2nd CSV file contains 2 columns ( Server Name, Value ).

This will have just 30 to 40 entries for all serves and value belongs to each server. The common value on both the CSV file is the server name.

I want to append the value from 2nd CSV file to the 1st one based on the sever name.

Final Output should look like this

Code:
ColumnA,ColumnB,ColumnC,ColumnD  
Client 1,XXXXX,Server A,value1 
Client 2,XXXXX,Server B,value2  
Client 3,XXXXX,Server C,value1
Client 4,XXXXX,Server C,value1
Client 5,XXXXX,Server B,value2
Client 6,XXXXX,Server C,value1

# 12  
Old 04-22-2013
Thanks very much for reposting the information. Building on what scrutinizer previously posted:
Code:
$ awk 'NR==FNR {$2=$2; A[$1]=$2} NR!=FNR {$3=$3; print $0, A[$3]}' FS=',' OFS=',' file2 file1
ColumnA,ColumnB,ColumnC,
Client 1,XXXXX,Server A,value 1
Client 2,XXXXX,Server B,value 2
Client 3,XXXXX,Server C,value 1
Client 4,XXXXX,Server C,value 1
Client 5,XXXXX,Server B,value 2
Client 6,XXXXX,Server C,value 1

I observed a problem with blanks at the ends of some of the lines confusing things, so I used the $2=$2 and $3=$3 to clean that up to my understanding, which I confess I learned from yoda.
# 13  
Old 04-22-2013
Hi hanson44,

Thanks much for your update, still it does not work. It is not printing the 4th column , it just printing the 1st CSV file again without the additional column from file2.
# 14  
Old 04-22-2013
Hit the wrong button. Wait a minute...

---------- Post updated at 09:20 PM ---------- Previous update was at 09:16 PM ----------

You're right. Sorry about that. Smilie

It does seem to involve the blanks at the end of the line. When those are not present in the file, it works fine, even without the $2=$2 assignments (gawk / linux):
Code:
$ cat file1
ColumnA,ColumnB,ColumnC
Client 1,XXXXX,Server A
Client 2,XXXXX,Server B
Client 3,XXXXX,Server C
Client 4,XXXXX,Server C
Client 5,XXXXX,Server B
Client 6,XXXXX,Server C

Code:
$ cat file2
ColumnA,CloumnB
Server A,value 1
Server B,value 2
Server C,value 1

Code:
$ awk 'NR==FNR {A[$1]=$2} NR!=FNR {print $0, A[$3]}' FS=',' OFS=',' file2 file1
ColumnA,ColumnB,ColumnC,
Client 1,XXXXX,Server A,value 1
Client 2,XXXXX,Server B,value 2
Client 3,XXXXX,Server C,value 1
Client 4,XXXXX,Server C,value 1
Client 5,XXXXX,Server B,value 2
Client 6,XXXXX,Server C,value 1

---------- Post updated at 09:27 PM ---------- Previous update was at 09:20 PM ----------

When I get rid of the blanks at the ends of the lines, it seems to work. GIve it a try:
Code:
$ awk 'NR==FNR {gsub (/ *$/, "", $2); A[$1]=$2} NR!=FNR {gsub (/ *$/, "", $3); print $0, A[$3]}' FS=',' OFS=',' file2 file1
ColumnA,ColumnB,ColumnC,
Client 1,XXXXX,Server A,value 1
Client 2,XXXXX,Server B,value 2
Client 3,XXXXX,Server C,value 1
Client 4,XXXXX,Server C,value 1
Client 5,XXXXX,Server B,value 2
Client 6,XXXXX,Server C,value 1

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

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

3. Shell Programming and Scripting

Remove ^L from csv not working...

hello... i have a requirement to convert a xls file to csv in RHEL 6.5 and ftp it to a windows location. i am using xls2csv utility to convert the file in linux. Input xls file attached... I have used below commands to convert the file to csv: xls2csv -x test.xls -s cp1252 -d 8859-1... (3 Replies)
Discussion started by: rhel65
3 Replies

4. UNIX for Dummies Questions & Answers

Remodelling the .csv file using the script

Hello All, I have a .csv file named as remark.csv The csv file content looks like below: Remark_Hello_1 Remark_Hello_2 Remark_Hello_3 Remark_Hello_4 Hello_World_FW_0001 X Hello_World_FW_0002 X X Hello_World_FW_0003 X X Hello_World_FW_0004 X X I... (5 Replies)
Discussion started by: suvendu4urs
5 Replies

5. Shell Programming and Scripting

Script to generate csv file

Hello; I need to generate a csv file that contains a list of all the files in a particular server (from the root directory ie: \) that have a permission stamp of 777. I would like to create the csv so that it contains the following: server name, file name, full path name where file exists,... (17 Replies)
Discussion started by: gvolpini
17 Replies

6. Shell Programming and Scripting

Conversion of below Tabs Tex file into CSV format file : shell script needed

Request if some one could provide me shell script that converts the below "input file" to "CSV format file" given Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- ----- ---------------------------------... (7 Replies)
Discussion started by: sreenath1037
7 Replies

7. Shell Programming and Scripting

How to convert a excel file to a .csv file from unix script

Hi I have a excel file in unix machine and have to convert it into a .csv file.I have to do this from a unix script.How do we do this? Thanks Abhinav (3 Replies)
Discussion started by: akashtcs
3 Replies

8. Shell Programming and Scripting

Shell Script to Load data into the database using a .csv file and .ctl file

Since i'm new to scripting i'm findind it difficult to code a script. The script has to be an executable with 2 paramters passed to it.The Parameters are 1. The Control file name(.ctl file) 2. The Data file name(.csv file) Does anybody have an idea about it? :confused: (3 Replies)
Discussion started by: Csmani
3 Replies

9. SCO

reg:rm -R is not working

I am installing the my package in SCO unix 5.0 .so it will create some temporary directories and files and same will delete.So , 'rm -R ' is not working in system .in the package only it has given this command,so now without this command working we can not go forward. so kindly help in this... (1 Reply)
Discussion started by: mokri_1980
1 Replies
Login or Register to Ask a Question