Combine csv's


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine csv's
# 1  
Old 08-12-2010
Combine csv's

All,

I would like to combine two csv's.

File1
Code:
text,text,value1
text,text,value1
text,text,value1

file2
Code:
text,text,value2
text,text,value2
text,text,value2

Note: Text fields will be the same in each file so can be taken from either

I would like to combine these so

file3

Code:
text,text,value1,value2
text,text,value1,value2
text,text,value1,value2

I had imagined I could do this in awk easily enough but cannot seem to understand syntax for processing two files at once. I would like to :

Code:
Read file1
Read file2
Output $1,$2,$3 ( all from file1), $3(from file2)

I cannot work out how to reference each field when read from a different file.

Any help/advice would be appreciated.

Thanks
# 2  
Old 08-12-2010
Hi,

Try this
Code:
 paste -d ',' file1 file2 | cut -d ',' -f1,2,3,6

OR
Code:
paste -d ',' file1 file2 | sed 's/,text,text//g'

# 3  
Old 08-12-2010
If both files are sorted the same way you can use
Code:
join -t, -1 1 -2 1 -o'1.1,1.2,1.3,2.3' text1.csv text2.csv


Last edited by pludi; 08-12-2010 at 08:25 AM.. Reason: replaced GNU option with POSIX standard
# 4  
Old 08-12-2010
IF YOU WANTS AWK PLEASE:
Code:
awk -F"," '{ getline line < "file1" } ; { print line, $3 }' file2


Last edited by Franklin52; 08-12-2010 at 09:03 AM.. Reason: Please use code tags, thank you!
# 5  
Old 08-12-2010
Works perfectly.

Thanks all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine and complete multiple CSV files based on 1 parameter

I have to create a new CSV file based on the value listed on the 3rd column from different CSV files. This is what I need: 1. I should substitute the first column from each file, excluding the headers, with the file name InputXX. 2. Then, I need to look for rows with 0 on the third column in... (7 Replies)
Discussion started by: Xterra
7 Replies

2. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

3. Shell Programming and Scripting

Compare 2 csv files in ksh and o/p the difference in a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 The... (7 Replies)
Discussion started by: Naresh101
7 Replies

4. Shell Programming and Scripting

Comparing 2 CSV files and sending the difference to a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 ... (1 Reply)
Discussion started by: Naresh101
1 Replies

5. Shell Programming and Scripting

Perl search csv fileA where two strings exist on another csv fileB

Hi I have two csv files, with the following formats: FileA.log: Application, This occured blah Application, That occured blah Application, Also this AnotherLog, Bob did this AnotherLog, Dave did that FileB.log: Uk, London, Application, datetime, LaterDateTime, Today it had'nt... (8 Replies)
Discussion started by: PerlNewbRP
8 Replies

6. Shell Programming and Scripting

Trying to combine 2 lines into one CSV format

Hi, I have the following test script I'm working with: date +"%m-%d-%y">>test.log grep eth0 /proc/net/dev | awk '{print ",",$2/1024/1024,",",$10/1024/1024}'>>test.log The output looks like this: 02-25-12 , 19.3581 , 84.2826 02-25-12 , 19.3587 , 84.283 02-25-12 , 19.3587 ,... (4 Replies)
Discussion started by: nbsparks
4 Replies

7. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

8. Shell Programming and Scripting

Combine two files and put it in .csv file

Hi Freinds I have two .txt file gem1.txt and gem2.txt, Sample gem1.txt abstract (1.0.0) actionmailer (2.3.5, 2.2.2) actionpack (2.3.5, 2.2.2) activerecord (2.3.5, 2.2.2) activerecord-oracle_enhanced-adapter (1.1.9) activerecord-sqlserver-adapter (2.3.4) activeresource (2.3.5, 2.2.2)... (3 Replies)
Discussion started by: ankit_view24
3 Replies

9. Shell Programming and Scripting

Combine Multiple text or csv files column-wise

Hi All I am trying to combine columns from multiple text files into a single file using paste command but the record length being unequal in the different files the data is running over to the closest empty cell on the left. Please see below. What can i do to resolve this ? File 1 File... (15 Replies)
Discussion started by: venky_ibm
15 Replies

10. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies
Login or Register to Ask a Question