How to use awk shell script to compare and match two files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use awk shell script to compare and match two files?
# 1  
Old 05-29-2013
How to use awk shell script to compare and match two files?

Basically, I have two files
Code:
dupestest.txt
  152,153
  192,193,194
  215,216
  290,291
  2279,2280 2282,2283

Code:
haftest.txt
  152,ABBOTS ROAD
  153,ABBOTS ROAD
  154,ABBOTS ROAD
  155,ABBOTS ROAD
  156,ABBOTS ROAD
  157,ABBOTS ROAD

I want to find the numbers in dupestest.txt in haftest.txt and produce this outcome: results.txt
Code:
152,ABBOTS ROAD,153 ABBOTS ROAD
192,ABBOTS ROAD, 193,ABBOTS ROAD, 194,ABBOTS ROAD

etc

(the numbers are just the count they are not premise numbers)


I have been told to try

Code:
awk -F, 'BEGIN{OFS=","} FNR==NR{a[$1]=$2; next} $1 in a || $2 in a{print $1, a[$1], $2, a[$2]}' haftest.txt dupestest.txt

But I want to do it in Notepad and save it as a AWK file instead of just a command line.I am totally new to AWK and have started with this:
Code:
BEGIN
{
OFS=",";

} 

FNR==NR

{

street[$1]=$2

}



End

Can anyone give me any advice?


It has to be in awk.
# 2  
Old 05-29-2013
I have a question regarding your required output:
Code:
152,ABBOTS ROAD,153 ABBOTS ROAD
192,ABBOTS ROAD, 193,ABBOTS ROAD, 194,ABBOTS ROAD

How you got 192, 193, 194 in the output? I don't see these numbers in file: haftest.txt
# 3  
Old 05-29-2013
Thats just a cut out of the HAF. The HAF is 4gb worth of data so it is in there just not on the bit I have posted.

---------- Post updated at 08:46 AM ---------- Previous update was at 08:45 AM ----------

All the data in the dupes will match up to a number in the HAF.
# 4  
Old 05-29-2013
I still don't get it!

If you are trying to join 2 files using 1st field, you could do something like:
Code:
awk '
        NR == FNR {
                D[$0]
                next
        }
        {
                for ( k in D )
                {
                        n = split ( k, V, "," )
                        for ( i = 1; i <= n; i++ )
                        {
                                split ( $0, T, "," )
                                if ( V[i] == T[1] )
                                {
                                        s = s ? s OFS $0 : $0
                                }
                        }
                        if ( s )
                                print s
                        s = ""
                }
        }
' OFS=, dupestest.txt haftest.txt

# 5  
Old 05-29-2013
This is more of the HAF:
Code:
272,ABBOTS ROAD                                                                   
273,ABBOTS ROAD                                                                   
274,ABBOTS ROAD                                                                   
275,ABBOTS ROAD                                                                   
276,ABBOTS ROAD                                                                   
277,ABBOTS ROAD                                                                   
278,ABBOTS ROAD                                                                   
279,ABBOTS ROAD                                                                   
280,ABBOTS ROAD                                                                   
281,ABBOTS ROAD                                                                   
282,ABBOTS ROAD                                                                   
283,ABBOTS ROAD                                                                   
284,ABBOTS ROAD                                                                   
285,ABBOTS ROAD                                                                   
286,ABBOTS ROAD                                                                   
287,ABBOTS ROAD                                                                   
288,ABBOTS ROAD                                                                   
289,ABBOTS ROAD                                                                   
290,ABBOTS ROAD                                                                   
291,ABBOTS ROAD                                                                   
292,ABBOTS ROAD                                                                   
293,ABBOTS ROAD                                                                   
294,ABBOTS ROAD                                                                   
295,ABBOTS ROAD                                                                   
296,ABBOTS ROAD                                                                   
297,ABBOTS ROAD                                                                   
298,ABBOTS ROAD                                                                   
299,ABBOTS ROAD                                                                   
300,ABBOTS ROAD                                                                   
301,ABBOTS ROAD

Its just a list of streets with a count at the beggining
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Shell script to compare two files for duplicate..??

Hi , I had a requirement to compare two files whether the two files are same or different .... like(files contaisn of two columns each) file1.txt 121343432213 1234 64564564646 2345 343423424234 2456 file2.txt 121343432213 1234 64564564646 2345 31231313123 3455 how to... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

Complex match of numbers between 2 files awk script

Hello to all, I hope some awk guru could help me. I have 2 input files: File1: Is the complete database File2: Contains some numbers which I want to compare File1: "NUMBERKEY","SERVICENAME","PARAMETERNAME","PARAMETERVALUE","ALTERNATENUMBERKEY"... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

4. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

5. Shell Programming and Scripting

Compare two files using shell script

Hi i want to compare two files and i need the o/p of only difference here the files file1 achilles aedxbepo aedxbwdm01 aedxbwdm02 albedo amarice ambrister anakin anton argon artephius asgard avatar aymara (10 Replies)
Discussion started by: venikathir
10 Replies

6. Shell Programming and Scripting

Shell script to compare two files

I have two files; file A and file B. I need all the entries of file A to be compared with file B line by line. If the entry exists on file B, then save those on file C; if no then save it on file D Note :- all the columns of the lines of file A need to be compared, except the last two columns... (8 Replies)
Discussion started by: ajiwww
8 Replies

7. Shell Programming and Scripting

awk script required to compare two files

HI friends, I have two files. File2 2nd column need to be compared with file1 3rd column.If ID match found, then save in different file as file1 data followed by file2. file1: No;age;id;name 1;24;37h;name1 2;22;67e;name2 4;48;9r;name3 6;67;9i8;name4 file2: exp;id;pos... (9 Replies)
Discussion started by: vasanth.vadalur
9 Replies

8. Shell Programming and Scripting

Shell Script to Compare Two Files

I have a directory with about 6 files that we receive regularly. these 6 files contain information for 3 different units, 2 for each unit. files related to a specific unit are named similarly with a change in number at the end of the file. the numbers should be sequential. for each grouping of... (3 Replies)
Discussion started by: scriptman237
3 Replies

9. UNIX for Dummies Questions & Answers

mawk script to compare 2 files and report where they match

I have two files and would like a report of where they match. Example of file1: 1 1 1 2 2 2 13 14 15 4 4 4 15 16 17 100 102 1004 56 57 890 Example of file2: 2 2 2 16 10 11 45 22 35 13 14 15 1001 1002 3456 100 102 1004 (1 Reply)
Discussion started by: kenneth.mcbride
1 Replies

10. UNIX and Linux Applications

How to compare two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file... (1 Reply)
Discussion started by: revenna
1 Replies
Login or Register to Ask a Question