Paste column from one file as column of


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Paste column from one file as column of
# 1  
Old 10-31-2014
Paste column from one file as column of

Any shortcuts for doing this? I need to cut the column 4 values from File1 and paste them as column4 values of File2, but only for the (first) same number of lines as File1 . All rows in File1 are contained in File2 in the exact same order, so the cut paste should work.

File1 (with header and 3 rows)

Code:
header1,header2,header3,header4
a,b,c,1
e,f,g,2
h,g,h,3


File2 (without header and 5 rows)
Code:
a,b,c
e,f,g
h,g,h
g,j,g,5
g,h,y,7

Output (with header and 5 rows)

Code:
header1,header2,header3,header4
a,b,c,1
e,f,g,2
h,g,h,3
g,j,g,5
g,h,y,7


My working code (without the header adding)

Code:
awk -F',' 'NR==FNR{a[$1","$2","$3","$4]=$0;next} $0 in a {$0=a[$0]}1' OFS=, File1 File2

But this is too slow for my huge files (20GB each), and a direct brute force method should work.
# 2  
Old 10-31-2014
How about
Code:
cat file1; tail -n+$(wc -l < file1) file2
header1,header2,header3,header4
a,b,c,1
e,f,g,2
h,g,h,3
g,j,g,5
g,h,y,7

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-31-2014
Or also maybe this?
Code:
awk -F, NF==4 file1 file2

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 10-31-2014
Code:
Might work for these trivial examples:
 
grep ',.*,.*,' file1 file2
 
More serious, assuming file1 only has header:
 
(echo;cat file2)|paste file1 -|sed '
  s/\t.*,.*,.*,/,/
  s/\t$//
 '

This User Gave Thanks to DGPickett For This Post:
# 5  
Old 10-31-2014
Code:
awk 'NR==FNR{n=NR-1; print; next} FNR>n' file1 file2

This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Paste columns based on common column: multiple files

Hi all, I've multiple files. In this case 5. Space separated columns. Each file has 12 columns. Each file has 300-400K lines. I want to get the output such that if a value in column 2 is present in all the files then get all the columns of that value and print it side by side. Desired output... (15 Replies)
Discussion started by: genome
15 Replies

3. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

4. Shell Programming and Scripting

Problems with awk (fatal error) and paste (two variables into one column-by-column)

Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script... 1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Discussion started by: vgbraymond
9 Replies

5. Shell Programming and Scripting

Copy a column and paste to other file question

Please help me. This is simple, but urgent problem for me. :( I have a two files file1 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 ..... file2 11 12 13 14 15 11 12 13 14 15 11 12 13 14 15 ..... 1) I hope to make a new file, file 3, that consists of 2nd... (2 Replies)
Discussion started by: exsonic
2 Replies

6. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

7. Shell Programming and Scripting

Paste value at specific column using sed

Hi I want to paste some value at specific column in a file using sed. say I want to add "Welcome to UNIX" from column 300 onwards in a file using sed. How to do it . ---------- Post updated at 11:18 AM ---------- Previous update was at 11:09 AM ---------- Adding more information : I... (14 Replies)
Discussion started by: dashing201
14 Replies

8. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

9. Shell Programming and Scripting

read file column and paste it in command

Hi Unix gurus I have a file containing 2 coloumns. I would like to do a script which reads the lines and executes a command like this: command <field1> parameters <field2> some more parameters Please let me know how you would do this without AWK, SED or any other mini language (for special... (5 Replies)
Discussion started by: BearCheese
5 Replies

10. Shell Programming and Scripting

paste each 10 lines of single column to several column

Hi, I need to paste each 10 lines of single column to several columns. Please, can anyone tell me how to write in awk? Input File: 22 34 36 12 17 19 15 11 89 99 56 38 29 (4 Replies)
Discussion started by: nica
4 Replies
Login or Register to Ask a Question