Compare to flat files using awk


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare to flat files using awk
# 1  
Compare to flat files using awk

compare to flat files using awk .but in 4th field contains non ordered substring. how to do that.
file1.txt
Code:
john|0.0|4|**:25;JP:50;UY:25

file2.txt
Code:
andy|0.0|4|JP:50;**:25;UY:25


Last edited by Franklin52; 05-24-2013 at 08:19 AM.. Reason: Please use code tags for data and code samples
# 2  
An approach using gawk:
Code:
gawk -F\| '
        NR == FNR {
                A[$1,$2,$3] = $4
                next
        }
        ($1,$2,$3) in A {
                i = split ( A[$1,$2,$3], I, ";" )
                j = split ( $4, J, ";" )
                asort( I )
                asort( J )
                if ( i == j )
                {
                        for ( k = 1; k <= j; k++ )
                        {
                                if ( I[k] == J[k] )
                                        F = 1
                                if ( I[k] != J[k] )
                                {
                                        F = 0
                                        break
                                }
                        }
                        if ( F )
                                print
                }
        }
' file1 file2

# 3  
after comparision print mismatch field ...

---------- Post updated at 03:41 PM ---------- Previous update was at 03:39 PM ----------

ex: file1 john|0.0|4|**:25;JP:50;UY:25
file2 andy|0.0|4|JP:50;**:25;ZY:25
in file2 instead of UY .. ZY is present ... in output error in 4th field needs to be print
# 4  
Quote:
Originally Posted by veeruasu
after comparision print mismatch field ...
ex: file1 john|0.0|4|**:25;JP:50;UY:25
file2 andy|0.0|4|JP:50;**:25;ZY:25
in file2 instead of UY .. ZY is present ... in output error in 4th field needs to be print
I don't understand! I do see that it is not just the 4th field that has mismatch, but also the 1st field (john/andy).

So how do you match records in both the files and identify mismatch in 4th field?

Tell us what criterion should be used to match records in both the files?

I would suggest you to post a sample input and desired output in code tags. Explain your requirement clearly and also show us what have you tried.
# 5  
ex: file1 john|0.0|4|**:25;JP:50;UY:25
file2 john|0.0|4|JP:50;**:25;UY:25
in above two files i need to check compare fourth field , but they are interchaged.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #811
Difficulty: Easy
RGBA stands for red green blue alpha.
True or False?

10 More Discussions You Might Find Interesting

1. HP-UX

Awk compare two files

Hi guys, I have 2 files: File1 ABC|2203|115.50 ABC|2288|328.12 ABC|2289|611.09 ABC|2290|698 DEF|1513|721.3 DEF|1514|40 DEF|1515|5 File2 ABC|2288|328.12 ABC|2289|666.08 ABC|2290|698.00 DEF|1513|721.30 (3 Replies)
Discussion started by: Eduardo Aceves
3 Replies

2. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

3. UNIX for Dummies Questions & Answers

Compare two flat files and update one based on the values in the other

Hi, I'm a newbie to scripting and am trying to compare two files using awk. The files are exactly the same dimensions. Where the first file has 0's I would like to create an updated version of the second file which has the corresponding elements set to zero also. eg: file1: 12345 1 2 0... (3 Replies)
Discussion started by: kasan0
3 Replies

4. Shell Programming and Scripting

awk to compare flat files and print output to another file

Hello, I am strugling from quite a some time to compare flat files with over 1 million records could anyone please help me. I want to compare two pipe delimited flat files, file1 with file2 and output the unmatched rows from file2 in file3 Sample File1: ... (9 Replies)
Discussion started by: suhaeb
9 Replies

5. UNIX for Dummies Questions & Answers

Using AWK to compare 2 files

Hi How can I use awk to compare specific columns in 2 files and print the difference. I currently have this: BEGIN { OFS = FS = "," } NR == FNR { b = $3 next } { e = "" for (x in b) { if (match ($1, x)) { if (RSTART == 1 && RLENGTH > length(e)) { e=x (2 Replies)
Discussion started by: ladyAnne
2 Replies

6. Shell Programming and Scripting

Compare 2 flat files

Hi Frnds, I have a flat file with millions of records. . Now I on this. (I prefer for AWK as its gives good performance.) Old_file.txt ------------------ 1 gopi ase . 2 arun pl 3 jack sutha .. 4 peter pm .. New_file.txt --------------- 4 peter pm .. .. ... (12 Replies)
Discussion started by: Gopal_Engg
12 Replies

7. Shell Programming and Scripting

Compare two files using awk

Hi. I'm new to awk and have searched for a solution to my problem, but haven't found the right answer yet. I have two files that look like this: file1 Delete,3105551234 Delete,3105551236 Delete,5625559876 Delete,5625556789 Delete,5625553456 Delete,5625551234 Delete,5625556956... (8 Replies)
Discussion started by: paul.o
8 Replies

8. Shell Programming and Scripting

Compare 2 flat files

Hi Gurus, I searched the forum but didnt get much info. I want to compare 2 files. 1)Newfile comes today with 2)Old file of previous day. The files are same ,just the new files might have new records sometimes. So I want to capture these new records in another file. Can anyone help... (5 Replies)
Discussion started by: ganesh123
5 Replies

9. Shell Programming and Scripting

How to compare two flat files and get changed data

Hi, I need to compare two flat files (yesterday & today's data) and get only the changed data from flat files. In flat file i dont have data column or anything its just a string data in flat file.Can any one please let me know the script With Regds Shashi (3 Replies)
Discussion started by: jtshashidhar
3 Replies

10. Shell Programming and Scripting

How to compare data in two flat files and update them?

Hi All, I am giving an example similar to the problem I have. I have two data files of 10 columns each in which fields are delimited by comma(,). I need to compare compare the two files using the uniq col(col3). If there are any records in file1 and are not in file2 then I have check the value... (3 Replies)
Discussion started by: rajus19
3 Replies

Featured Tech Videos