Awk Comparison of 2 specific files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Comparison of 2 specific files
# 1  
Old 02-18-2009
Awk Comparison of 2 specific files

Hi Everybody,

I know the topic sounds familiar but I just couldn't adapt or find the right code that solves my particular issue. I really hope you can help.

I would like to compare 2 files in an awk script. Both files have different paths.

The awk script call should look like that awk -f AWKSCRIPT parameter1.

The files look as follows:

File_old

Header
record1
record2
record3
Footer

File_new

Header
record1
record2
record3
Footer

First I would like to test if both files have the same amount of records.

If NO, return a code("NOMATCH") that can be further processed by a shell script. AWK Script is supposed to be called from within a bourne shell script.

If YES, please compare just the records Line by Line. I mean compare:

File_old:record1 with File_new:record1
File_old:record2 with File_new:record2

and so on...

If one of those comparisons doesn't match, return again a code("NOMATCH") that can be further processed by the calling bourne shell script. If all of the records match with on another return code("MATCH") to calling bourne shell script.

I hope I didn't describe it to complicated.

I would be very grateful for all your help.

Thanks in advance,

hhoosscchhii
# 2  
Old 02-19-2009
Hi,

I think its better to use shell command 'comm' or 'diff' for this requirment.
It will be like

DiffCount=`comm -3 file1 file2|wc -l`
if [ $DiffCount -gt 0 ]
then
echo "Files are not same"
fi

Regards,

Ranjith
# 3  
Old 02-19-2009
Hi ,

Hope,this also can help you....

if you have two files & only one column if you want to compare then..

step 1:

paste -d, file1 file2 > outputfile

step 2:

awk -F, '{if($1 == $2) {print "MATCH"} else {print "NO match"}}' outputfile

Now you will get output like below:

MATCH
MATCH
MATCH
MATCH
MATCH

Thanks
ShaSmilie
# 4  
Old 02-19-2009
Thanks guys already

Sorry I am crazy busy right now. I would have liked to reply much earlier already. But as the title is mentioning I am busy like hell. Thanks for your help, I will try it out and let you guys know what happend.

Like I said, Thanks anyway and take care for now

Sebastian
# 5  
Old 02-23-2009
Something special

Thanks again guys for your response, but the files I would like to compare have something special to it.

Like I already mentioned, both files have the same layout:

Header
Record1
Record2
...
Footer

The header is always different, so nothing to be focused on.
The records first 25 signs are always different too.
Now the important part. After sign 25 of each record I would like to start comparing with the corresponding record in the second file.

if record1.file1 after sign25 equals record1.file2 after sign25
set return value 0
else set return value 1

and so on for all remaining records.

Note: I can't do anything about the format of the file. I get it that why and I have to work with it.

Your help would be very much appreciated

Thx,


Sebastian
# 6  
Old 02-25-2009
Please, does anybody have an idea?

I wouldn't ask if I wouldn't have tried it first myself.

Thanks,

SebastianSmilie
# 7  
Old 02-25-2009
Read input line from certain position

I might be able to figure out a solution for my whole problem, if somebody
could just tell me how to read an input line from a certain position.

f.ex.

input line

012345678910

I would like to use that line starting from position 5 and ignore the first 5 positions.

Any Ideas?

Thanks a lot.

Sebastian
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk comparison using multiple files

Hi, I have 2 files, I need to use column of file1 and do a comparison on file2 column 1 and print the mismatch is file3 as mentioned below. Kindly consider that file 1 is having uniq key(column) whereas in file2 we have multiple duplicates (like 44). These duplicates should not come in... (2 Replies)
Discussion started by: grv
2 Replies

2. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

3. Shell Programming and Scripting

Awk: Replacement using 2 diff files input and comparison

Requirement: If $5(date field) in ipfile is less than $7(date field) in deact file & $1 of ipfile is present in deactfile then $1 to be replaced by $2,$3,$4,$5,$6 of deact file else if $5(date field) in ipfile is greater than $7(date field) in actfile & $1 of ipfile is present in actfile then... (5 Replies)
Discussion started by: siramitsharma
5 Replies

4. Shell Programming and Scripting

awk or sed to find specific column from different files

Hi everybody, I have a folder with many files: Files with 8 columns: X 123 A B C D E F And files with 7 columns: X1234 A B C D E F I am trying to find a way to extract the 5th column when the files have eight columns, or the 4th column when the files have... (3 Replies)
Discussion started by: Tzole
3 Replies

5. Shell Programming and Scripting

comparison of 2 files using unix or awk

Hello, I have 2 files and I want them to be compared in a specific fashion file1: A_1200_1250 A_1251_1300 B_1301_1350 B_1351_1400 B_1401_1450 C_1451_1500 and so on... file2: 1210 1305 1260 1295 1400 1500 1450 1495 Now The script should look for "1200" from A_1200_1250 of... (8 Replies)
Discussion started by: Diya123
8 Replies

6. UNIX for Dummies Questions & Answers

df -> output files; comparison using awk or...

:wall: I am trying to do the following using awk (is that the best way?): Read 2 files created from the output of df (say, on different days) and compare the entries using the 1st (FileSys) and 6th (Mount) fields to see if the size has changed. Output (at least), to a new file (some header... (2 Replies)
Discussion started by: renata
2 Replies

7. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

8. Shell Programming and Scripting

Ignore some lines with specific words from file comparison

Hi all, I need help in doing this scenario. I have two files with multiple lines. I want to compare these two files but ignoring the lines which have words like Tran, Loc, Addr, Charge. Also if i have a word Credit in line, i want to tokenize (i.e string after character " ... (2 Replies)
Discussion started by: jakSun8
2 Replies

9. Shell Programming and Scripting

Comparison of two files in awk

Hi, I have two files file1 and file2 delimited by semicolon, And I want to compare column 2 and column3 of file1 to column3 and column 4 in file2. file1 -------- abc;cef;155.67;143_34; def;fgh;146.55;123.3; frg;hff;134.67;; yyy;fgh;134.78;35_45; file 2 --------- abc;cef;155.09;;... (12 Replies)
Discussion started by: jerome Sukumar
12 Replies

10. Shell Programming and Scripting

String Comparison between two files using awk

I have two files with field seperator as "~". File A: 12~13~14~15 File B: 22~22~32~11 i want to calculate the difference between two files and than calculate the percentage difference and output it to a new file. How do i do this using awk. Also please suggest GOOD awk tutorials. Thank... (7 Replies)
Discussion started by: rudoraj
7 Replies
Login or Register to Ask a Question