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
# 1  
Old 04-20-2013
Reg - Working on a CSV File in a script

Dear All,

I have 2 CSV files and want to create a new CSV based on a common value in both of the CSV file.

Code:
ColumnA  ColumnB ColumnC 
Client 1     XXXXX  Server A
Client 2     XXXXX  Server B 
Client 3     XXXXX   Server C

2nd CSV file

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


The common value on the both the CSV file woulld be the server value ( Column C on 1st and Column A on 2nd CSV file).

Final Output File

Code:
ColumnA  ColumnB ColumnC   CoulmnD
Client 1     XXXXX  Server A    Value 1
Client 2     XXXXX  Server B     Value 2 
Client 3     XXXXX   Server C    value1

Can someone help me how I can achieve with this with awk or sed ? Also if you know any website where I can learn about awk or sed i detail ?

Thanks in advance for any help !!!!
# 2  
Old 04-20-2013
This is the code used for the above mentioned purpose
Code:
awk 'FNR==NR{a[$1]=$0 FS $0;next}{ print $0, a[$1]}' file1 file2

I worked it with text files not sure about csv files
This User Gave Thanks to anurupa777 For This Post:
# 3  
Old 04-20-2013
Try:
Code:
awk 'NR==FNR{A[$1]=$2} $3 in A{print $0, A[$3]}' FS='\t' OFS='\t'  file2 file1

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-20-2013
Since you say CSV, I think the format should be like:
Code:
$ cat file1
"ColumnA","ColumnB","ColumnC"
"Client 1","XXXXX","Server A"
"Client 2","XXXXX","Server B"
"Client 3","XXXXX","Server C"

Code:
$ cat file2
"ColumnA","ColumnB"
"Server A",1
"Server B",2
"Server C",1

Alternatively, you could leave out the "double quotes" in this case.
# 5  
Old 04-22-2013
I tried both from above but didn't work. Any other solutions ?
# 6  
Old 04-22-2013
It would help if you could repost the current situation (current input files and expected output), and what happened when you tried the solutions. Smilie
# 7  
Old 04-22-2013
Quote:
Originally Posted by rrb2009
I tried both from above but didn't work. Any other solutions ?
What did not work? What is your OS and version?
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