Merging fileds from 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging fileds from 2 files
# 1  
Old 04-19-2005
Merging fields from 2 files

I have 2 files - fileA and fileB
I need to match the first field of the 2 files then output some of the fields from fileA on the same line as certain fields from fileB.
There will be instances where fileB does not have a match for first field in fileA
Thanks in advance

Last edited by Mudshark; 04-19-2005 at 10:35 AM..
# 2  
Old 04-19-2005
if you could elaborate on what exactly needed to be done with the sample input and the expected output, someone here could probably have helped you.

As a starting point I would look into awk's associative arrays.
# 3  
Old 04-19-2005
You also might want to look at the "join" command. It has a man page.
# 4  
Old 04-19-2005
More info

Apologies vgersh99 I'll try to elaborate.
fileA would look something lke this
500,22,1001,16,22,0,4
501,23,2004,16,26,1,9
502,44,1003,18,0,0,10
503,43,1000,22,1,14,15
504,40,1006,20,0,22,22
505,32,1009,21,11,32,88

fileB might look like this:
500,Florence,30,477
501,Zebedee,28,505
503,Dougal,80,609
504,Brian,70,300


As you can see, 502 and 505 are missing from fileB and therefore the match would fail. There wouldn't be an instance where fileB contained a value in 1st fields which doesn't appear in fileA.
The output should contain the first 4 fields from fileA together with fields 3 and 4 from fileB on the same line. Where there is no match in fileB these values should contain zero.
EG
500,22,1001,16,30,477
501,23,2004,16,28,505
502,44,1003,18,0,0
503,43,1000,22,80,609
504,40,1006,20,70,300
505,32,1009,21,0,0

Hope this makes more sense - thanks again
# 5  
Old 04-19-2005
nawk -f mud.awk fileB fileA

here's mud.awk:
Code:
BEGIN {
  FS=OFS=","
  defValues="0" OFS "0"
}

FNR == NR {
  arr[$1] = $3 OFS $4
  next
}

{
   NF=5
   $5 = ( $1 in arr ) ? arr[$1] : defValues
   print
}

# 6  
Old 04-19-2005
Maximum Index for array?

Thanks vgersh99 - That works fine. One further question - is there a maximum number of indices which can be applied to an array - my real-world application will work on files which may have thousands ( or 10s of thousands) of records.
Thanks Again
# 7  
Old 04-19-2005
Quote:
Originally Posted by Mudshark
Thanks vgersh99 - That works fine. One further question - is there a maximum number of indices which can be applied to an array - my real-world application will work on files which may have thousands ( or 10s of thousands) of records.
Thanks Again
this is limited by the physical memory [for the well-behaved awk's].
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging two files

Hi All , I have a scenario where we need to combine two files . Below are the sample files and expected output , File 1: 1|ab 1|ac 1|ae 2|ad 2|ac File 2: 1|xy 1|fc 2|gh 2|ku Output file : 1|ab|xy (3 Replies)
Discussion started by: saj
3 Replies

2. Shell Programming and Scripting

Merging two files

Guys, I am having little problem with getting a daily report! The daily process that I do is as follows 1. Unload Header for the report from the systables to one unl file, say Header.unl 2. Unload the data from the required table/tables to another unl file, say Data.unl 3. Send a... (2 Replies)
Discussion started by: PikK45
2 Replies

3. Shell Programming and Scripting

Merging two files with same name

Hello all, I have limited experience in shell scripting. Here goes my question: I have two directories that have same number of files with same file names i.e. consider 2 directories A and B. Both directories have files 1.txt, 2.txt...... I need to merge the file 1.txt of A with file 1.txt... (5 Replies)
Discussion started by: jaysean
5 Replies

4. Shell Programming and Scripting

Merging files

I have two files file 1 containing x rows and 1 column file 2 containing x rows and 1 column I want to merge both the files and add a comma between the two eg plz guide (1 Reply)
Discussion started by: test_user
1 Replies

5. UNIX for Dummies Questions & Answers

Merging two files

Hi, I have two files a.txt and b.txt. a.txt 1 2 3 4 b.txt a b c d e I want to generate a file c.txt by merging these two file and the resultant file would contain c.txt 1 (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

6. Shell Programming and Scripting

merging of files.

Hi, I want to merge the two files on the basis of columns like... file 1 Data Key A 12 B 13 file2 Data Value A A1 A A2 B B1 B B2 (5 Replies)
Discussion started by: clx
5 Replies

7. Shell Programming and Scripting

merging two files

Hi everyone, I have two files which will be exactly same at first. After sometime there will be inserts in one file. My problem is how to reflect these changes in second file also. I found out that any compare and merge utility would do the job like, GNU " sdiff " command. But the... (14 Replies)
Discussion started by: rameshonline
14 Replies

8. Shell Programming and Scripting

Merging 2 files

Hi, I have got two files 1.txt 1111|apple| 2222|orange| 2.txt 1111|1234|000000000004356| 1111|1234|000000001111| 1111|1234|002000011112| 2222|5678|000000002222| 2222|9102|000000002222| I need to merge these two so that my out put looks like below: Search code being used should be... (4 Replies)
Discussion started by: jisha
4 Replies

9. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

10. UNIX for Dummies Questions & Answers

Merging two files

Hi I have a requirement like this. I have two files This is how data1.txt looks: EI3171280 38640658501 NENN2005-12-129999-12-312005-12-12HALL NANCY 344 CHENEY HIGHWAY ... (4 Replies)
Discussion started by: venommaker
4 Replies
Login or Register to Ask a Question