Joining three files with uniq no.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining three files with uniq no.
# 1  
Old 11-22-2012
Joining three files with uniq no.

Hi I have three files which looks like as below with first 11byte is unique as a serial no.(eg.0X000000001,0X000000002,0X000000003)

File1
Code:
0X000000001|00XXX|D3|F2|C3|A6|1|
0X000000002|00XXX|E3|G2|D3|B6|1|
0X000000003|00XXX|F3|H2|E3|C6|1|

File2
Code:
0X000000001|00XXX|D3|F2|
0X000000002|00XXX|E3|G2|
0X000000003|00XXX|F3|H2|

File3
Code:
0X000000001|00XXX|D3|F2|X4|
0X000000002|00XXX|E3|G2|Y4|
0X000000003|00XXX|F3|H2|Z4|

output should be
Code:
0X000000001|00XXX|D3|F2|C3|A6|1|00XXX|D3|F2|00XXX|D3|F2|X4|
0X000000002|00XXX|E3|G2|D3|B6|1|00XXX|E3|G2|00XXX|E3|G2|Y4|
0X000000003|00XXX|F3|H2|E3|C6|1|00XXX|F3|H2|00XXX|F3|H2|Z4|

Serial number followed by content from first file,second file and third file.

Last edited by Franklin52; 11-22-2012 at 10:22 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-22-2012
Code:
awk -F '|'  'FILENAME=="File1"  {arr[$1]=$0;next }
                FILENAME=="File2" ||
                     FILENAME=="File3"   {arr$[1]=arr[$1} $substr($0, index($0, "|") ) }                
                END{for(i in arr){ print arr[i] }  '   File1 File2  File3

# 3  
Old 11-22-2012
Code:
paste -d File1 File2  File3

# 4  
Old 11-22-2012
Or (with assumptions):
Code:
awk '{printf("%s",$0)
getline < f2
match($0,/[^|]*[|][^|]*[|]/)
printf("%s",substr($0,RSTART+RLENGTH))
getline < f3
match($0,/[^|]*[|][^|]*[|]/)
printf("%s",substr($0,RSTART+RLENGTH))
printf "\n"}' f2=file2 f3=file3 file1

# 5  
Old 11-23-2012
OR

Code:
awk -F "|" '
    f==1{X[FNR]=$0}
    f==2{sub($1"\\|","",$0);X[FNR]=X[FNR]""$0}
    f==3{sub($1"\\|","",$0);print X[FNR]""$0}' f=1 file1 f=2 file2 f=3 file3

# 6  
Old 11-23-2012
i am geting some error while using below command
Code:
awk -F '|'  'FILENAME=="File1"  {arr[$1]=$0;next }
                FILENAME=="File2" ||
                     FILENAME=="File3"   {arr$[1]=arr[$1} $substr($0, index($0, "|") ) }                
                END{for(i in arr){ print arr[i] }  '   File1 File2  File3

i am using
Code:
cat f1.txt f2.txt f3.txt > newfile
awk 'BEGIN{OFS=FS="|"}
{
  org=$1
  $1=""
  a[org]=a[org]""$0  
} 
END {
 for (i in a) print i,a[i]
}' newfile

but now problem is each file contains 300000 record so above command is not working properly...
can anyone help me?

Last edited by Franklin52; 11-23-2012 at 04:10 AM.. Reason: Please use code tags for data and code samples
# 7  
Old 11-24-2012
With input files so big, it's more efficient to use join:
Code:
join -t\| -j1 -o1.1,1.2,1.3,1.4,1.5,1.6,1.7,2.2,2.3,2.4 f1 f2 |join -t\| -j1 - f3

If the input files were not sorted, sort first before joining. The long list of fields after -o is to get rid of the trailing '|' from f1, otherwise, the whole -o... can be omitted.
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

Joining 2 Files

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 ..... File "B" (column names: TYPE DEVICE NUMBER SIZE) TYPE DEVICE NUMBER SIZE 1750500 hdisk2 100A 16384 1750500 hdisk3 ... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

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

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

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

Using uniq on log files

I have this log file which I need to count the number of repeated line and do some manipulation. test.log: June 3 03:33:38 test 1 June 3 10:31:22 test 2 June 3 10:32:22 test 2 June 3 10:33:22 test 3 June 3 10:33:22 test 3 June 3 10:34:22 test 4 June 3 10:35:22 test 5 ... (4 Replies)
Discussion started by: jazzaddict
4 Replies

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

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

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