Unix Bash: substitute columns in .csv using other .csv columns


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Unix Bash: substitute columns in .csv using other .csv columns
# 1  
Old 01-04-2011
Unix Bash: substitute columns in .csv using other .csv columns

Hi All,

I have two .csv's

input.csv having values as (7 columns)
Code:
ABC,A19907103,ABC DEV YUNG,2.17,1000,2157,07/07/2006
XYZ,H00213850,MM TRUP HILL,38.38,580,23308,31/08/2010

output.csv having (25 columns)
Code:
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y

I want to put values of input.csv into output.csv at different column positions

Mapping:
Code:
1 2 3 4 5 6 7
7 11 13 15 20 21

Can you please help suggest how this can be done. I tried using awk but unsuccessful

If I just try to substitute output.csv's Column7 with input.csv's column 1.. but get no output with below command.
Code:
/usr/xpg4/bin/awk 'BEGIN {FS="," ; OFS=","} FNR == NR {a[FNR] = $B; next} $A = a[FNR]' B=2 A=4 input.csv output.csv

Please help or provide a good link. Thanks a lot


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by Franklin52; 01-04-2011 at 09:06 AM..
# 2  
Old 01-04-2011
Post desired output for those two files.
# 3  
Old 01-04-2011
Thanks for prompt reply..

Output is desired as:
Code:
A,B,C,D,E,F,ABC,H,I,J,A19907103,L,ABC DEV YUNG,N,2.17,P,Q,R,S,1000,2157,07/07/2006,W,X,Y
A,B,C,D,E,F,XYZ,H,I,J,H00213850,L,MM TRUP HILL,N,38.38,P,Q,R,S,580,23308,31/08/2010,W,X,Y

thus subsituting 7 columns from input.csv in below order
Code:
7 11 13 15 20 21 22


Last edited by Franklin52; 01-04-2011 at 09:29 AM.. Reason: Please use code tags.
# 4  
Old 01-04-2011
Try this:
Code:
awk -F, '
BEGIN{n=split("7 11 13 15 20 21 22", a, " ")}
{
  getline line < "input.csv"
  split(line,b)
  for(i=1;i<=n;i++) {
    $a[i]=b[i]
  }
}1' OFS="," output.csv

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 01-05-2011
Many thanks Franklin, the code worked as it is and successfully substituted Columns.
Thanks a lot for such quick help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX Command to Match columns from two csv files

I am joining two CSV files based on 'Server_Name' column, 1st column of first file and 2nd column of second file. If matches, output 1st and 2nd column from first file, 3rd,4th,5th,6th columns from second file. I am expecting output CSV file as below. Could you please send me help me with... (6 Replies)
Discussion started by: Anadmbt
6 Replies

2. UNIX for Advanced & Expert Users

Replace columns in .csv using other .csv columns

Hi, I have 2 csv files with 15 000 lines, which looks like this: Daily.csv "CODE","BRAND","DESIGNER","SIZE","TYPE","GENDER","SET","DESCRIPTION","IMAGE","COST","WEIGHT","MSRP","UPC" "M-1001","212","Caroline Her","1.7 oz","EDT... (4 Replies)
Discussion started by: olivieraz
4 Replies

3. Shell Programming and Scripting

Import 2 columns from 8 .csv files into pandas df (side by side) and write a new csv

I have 8 .csv files with 16 columns and "n" rows with no Header. I want to parse each of these .csv and get column and put the data into a new.csv. Once this is done, the new.csv should have 16 columns (2 from each input.csv) and "n" rows. Now, I want to just take the average of Column from... (3 Replies)
Discussion started by: Zam_1234
3 Replies

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

5. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

6. Shell Programming and Scripting

Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison. Thanks in advance for your help. File A A,B,C,45,46... (2 Replies)
Discussion started by: ady_koolz
2 Replies

7. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

8. Shell Programming and Scripting

CSV Sorting on only particular columns

Hello! So ive been presented with this comma-delimited file: I need a print to look as below " lastname, phone_number, zip for every person with a last name starting with the letter H, I only with a 650-area code phone number. output should be sorted by reverse ZIP code " I only have... (5 Replies)
Discussion started by: strangemachine
5 Replies

9. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

10. UNIX for Advanced & Expert Users

Adding columns in csv

I have the following data in FILE1.CSV: code: Amount1: Amount2: xxxxx ,, 200 ,,400 yyxxa ,,200 bbcgu ,,2500 ,,300 i want to be able to produce the following FILE2.CSV: code: Amount xxxxx ,, 600... (7 Replies)
Discussion started by: chachabronson
7 Replies
Login or Register to Ask a Question