Compare to flat files using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare to flat files using awk
# 1  
Old 05-24-2013
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 07:19 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 05-24-2013
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  
Old 05-24-2013
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  
Old 05-24-2013
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  
Old 07-09-2013
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 to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[awk] Compare two files

HI!! I am trying to compare two files using AWK but I have some problems. I need to count how many times letters are used in two texts. This is my script { long=length($0) for (i=1;i<=long;i++) { aux=substr($0,i,1) if ( aux != " " && aux != "" ) ... (7 Replies)
Discussion started by: ettore8888
7 Replies

2. Shell Programming and Scripting

awk compare files

I have a below requirement and trying to compare the files using awk File 1 - Already stored on a prev day id | text | email id --------------------------------- 89564|this is line 1 | xyz@sample.txt 985384|this is line 2 | abc@sample.txt 657342|this is line 3 |... (3 Replies)
Discussion started by: rakesh_411
3 Replies

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

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

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

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

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

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
Login or Register to Ask a Question