Joining 2 Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining 2 Files
# 1  
Old 02-05-2013
Joining 2 Files

HTML Code:
File "A" (column names: Nickname	Number	GB)
Nickname	Number	GB
PROD_DB0034	100A	16
ASMIL1B_DATA_003	100B	16
PSPROD_0000	1014	36
PSPROD_0001	100D	223
.....
HTML Code:
File "B" (column names: TYPE    DEVICE NUMBER  SIZE)
TYPE    DEVICE NUMBER SIZE
1750500 hdisk2  100A    16384
1750500 hdisk3  1010    73728
1750500 hdisk5  1015    36864
1750500 hdisk4  1014    36864
I have two files above. On both files, "Number" (on A) and "NUMBER " (on B) are the join connector for these 2 file.

The output I want after the join is:

HTML Code:
Nickname	Number	GB    TYPE    DEVICE NUMBER SIZE    REMARK
PROD_DB0034	100A	16    1750500 hdisk2  100A    16384
ASMIL1B_DATA_003	100B	16  -- -- -- --                       (no match)
-- -- -- --                         1750500 hdisk3  1010    73728   (no match)
PSPROD_0000	1014	36     1750500 hdisk4  1014    36864
-- -- -- --                          1750500 hdisk5  1015    36864  (no match)
PSPROD_0001	100D	223  -- -- -- --                              (no match)
Please advise.

Last edited by jim mcnamara; 02-05-2013 at 08:37 PM..
# 2  
Old 02-05-2013
Using awk:

Code:
awk '
  FNR==NR{F[toupper($2)]=$0; next}
  FNR==1{$0=$0"    REMARK"}
  $3 in F {print F[$3], $0; delete F[$3]; next}
  {print "-- -- -- --", $0, "(no match)"}
  END{ for (l in F) print F[l], "-- -- -- -- (no match)"}' fileA fileB

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 02-06-2013
Try

Code:
 
awk 'NR==1{s=$0}
 NR==FNR && NR>1{A[$2]=$0;next}
 FNR==1 && NR>1 {print s"\t"$0}
 FNR>1{if(A[$3]){print A[$3]"\t"$0;delete A[$3]}else{print "-- -- -- -- \t\t\t" $0 "(no match)"}}
 END{for(i in A){if(A[i]){print A[i] "\t -- -- -- -- "}}}' file1 file2

This User Gave Thanks to pamu For This Post:
# 4  
Old 02-06-2013
why can't we use paste command

Code:
 paste $file1 $file2

# 5  
Old 02-06-2013
Thank you! It works perfect!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help me in joining two files

I have two files with the below contents : sampleoutput3.txt 20150202;hostname1 20150223;hostname2 20150716;hostname3 sampleoutput1.txt hostname;packages_out_of_date;errata_out_of_date; hostname1;11;0; hostnamea;12;0; hostnameb;11;0; hostnamec;95;38; hostnamed;440;358;... (2 Replies)
Discussion started by: rahul2662
2 Replies

2. Shell Programming and Scripting

Help with joining files and adding headers to files

Hi, I have about 20 tab delimited text files that have non sequential numbering such as: UCD2.summary.txt UCD45.summary.txt UCD56.summery.txt The first column of each file has the same number of lines and content. The next 2 column have data points: i.e UCD2.summary.txt: a 8.9 ... (8 Replies)
Discussion started by: rrdavis
8 Replies

3. Shell Programming and Scripting

Joining two files into one

Hi experts, I'm quite newbie here!! I have two seperate files. Contents of file like below File 1: 6213019212001 8063737 File:2 15703784 I want to join these two files into one where content will be File 3: 6213019212001 8063737 15703784 Regards, Ray Seilden (1 Reply)
Discussion started by: RayanS
1 Replies

4. UNIX for Dummies Questions & Answers

Joining two files

I have two comma separated files. I want to join those filesa nd put the result in separate file. smaple data are: file1: A1,1,100 A2,1,200 B1,2,100 B2,2,200 file2 1,50 1,25 1,25 1,100 1,100 2,50 2,50 (10 Replies)
Discussion started by: pandeesh
10 Replies

5. Shell Programming and Scripting

Joining Three Files

Hi guys, I have three files which needs to be joined to a single file. File 1: Col a, Col b, Col c File 2: Col 1a, Col 1b File 3: Col 2a, Col 2b Output: Col 1a, Col 2a, Col a, Col b, Col c. All the files are comma delimited. I need to join Col b with Col 1b and need to... (17 Replies)
Discussion started by: mac4rfree
17 Replies

6. Shell Programming and Scripting

joining two or more files

i have three files file a has contents 123 234 238 file b has contents 189 567 567 and file c has contents qwe ert ery (1 Reply)
Discussion started by: tomjones
1 Replies

7. Shell Programming and Scripting

Joining files

Hi, Whats the unix function to join multiple files? is it cat? so I have multiple files in the same format and I want to join then by row eg. FILE1 1 3 1 3 1 3 1 3 FILE2 2 4 2 4 2 4 (1 Reply)
Discussion started by: kylle345
1 Replies

8. Shell Programming and Scripting

Help with joining two files

Greetings, all. I've got a project that requires I join two data files together, then do some processing and output. Everything must be done in a shell script, using standard unix tools. The files look like the following: File_1 Layout: Acct#,Subacct#,Descrip Sample: ... (3 Replies)
Discussion started by: rjlohman
3 Replies

9. UNIX for Dummies Questions & Answers

joining files

Hi, Could anyone help me ? I'm trying to join two files, but no common field are on them. So I think on generate \000\ sequence to add for each line on both files, so then will be able to join these files. Any idea? Thanks in advance, (2 Replies)
Discussion started by: Manu
2 Replies

10. UNIX for Dummies Questions & Answers

joining 2 files

Hi, I have two files that I need to find difference between. Do I use diff or join? If join, how do I use it? thanks, webtekie (1 Reply)
Discussion started by: webtekie
1 Replies
Login or Register to Ask a Question