compare 2 files and extract the data which is not present in other file with condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare 2 files and extract the data which is not present in other file with condition
# 1  
Old 02-03-2012
Bug compare 2 files and extract the data which is not present in other file with condition

I have 2 files whose data's are as follows :

fileA
Code:
00    lieferungen   
00    attractiop
01    done
02    forness
03    rasp
04    alwaysisng
04    funny
05     done1

fileB
Code:
alwayssng
dkhf
fdgdfg
dfgdg
sdjkgkdfjg
funny
rasp
done1

I want to compare from fileA 2nd column values with rows of fileB . if that value is not present in fileB then write that value in other file. but only those value of fileA is compared with fileB whose fileA value of 1st column is greater and equal than 03.

eg 1st value picked up from file A "lieferungen" whose 1st column is 00 . so it will not be compare..
next value like "funny" whose 1st value 04 is greater than 3 so it is compared with fileB
as it is not present in file B . it is written in third file.

output
Code:
fileC
03    rasp
04    funny
05    done1


i have a huge database of more than 80000 records. plz help if it can be done using awk

Last edited by rajniman; 02-07-2012 at 09:39 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-03-2012
Code:
awk 'NR==FNR{a[$0]++;next}a[$2] && $1>=3' fileB fileA

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 02-06-2012
Hi ahamad ,
thnaks for your reply.
But how do i write answer in third file.?
# 4  
Old 02-06-2012
This also works
Code:
grep -f fileB fileA | awk ' $1 >= 3' > fileC


Last edited by Franklin52; 02-06-2012 at 09:12 AM.. Reason: Please use code tags for data and code samples, thank you
This User Gave Thanks to kalpeer For This Post:
# 5  
Old 02-07-2012
thanks kalpeer for replying

i have one more file as
FileD
Code:
funny       mou1
funny       mou2
raspe       mou4
damn       mou1

this file is a csv with /t.
i want to find all values with the first column of this file in fileB and then check for in that line if the second column vvalues are present or not .

for eg suppose in fileB there is a line
Code:
funnymou1:20112
funnymou2:34470
raspmou3:nhdhv
raspmou4:38748

fileD is 1st content is of 1st column is "funny"
so funny would be checked in fileB if found then "funny" second column will be checked i.e mou1 will be checked in that line if present than ok. if not present than it will be written in other fileE.if first column of fileD is present than only second column of fileD is checked in that line .if first column field can be present in more than one rows in fileB all lines of fileB should be then compared with second column of fileD if found then not written ... if not found then written in fileE
so for
raspe mou4 : as raspe is not present in fileB it will be written in fileE as output

so
funny mou1
is present so it will not be written in fileE
next
funny mou2 is also present in fileB so not writen
now
raspe mou4
damn mou1 will be wriiten in fileE as it is not present in fileB.

o/p of fileE
Code:
funny   mou1
funny   mou2

can it be done using awk ... i have records more than 80000

---------- Post updated at 09:22 AM ---------- Previous update was at 08:52 AM ----------

hi kalpeer,

the code
grep -f fileB fileA | awk ' $1 >= 3' > fileC
is not working Smilie
as fileA contents may be present in a line of fileB(means in between of that line in fileB and not as a whole) but not vice versa..
means total line of fileB cannot be present in fileA
if fileB contains
2011890done1
3235235funny

as fileA
03 done
04 funny

so the code isnt working ..
please help!

Last edited by rajniman; 02-07-2012 at 10:25 AM.. Reason: some chages
# 6  
Old 02-07-2012
You can redirect the output to a third file for example fileC:

Code:
awk 'NR==FNR{a[$0]++;next}a[$2] && $1>=3' fileB fileA >fileC

Code:
awk 'NR==FNR{a[$0];next}($2 in a)&& $1>=3' fileB fileA >fileC

If your file really is <tab> separated (which is not the case of the fileA you've initially posted) :
Code:
awk -F"\t" 'NR==FNR{a[$0];next}($2 in a)&& $1>=3' fileB fileA >fileC


Last edited by ctsgnb; 02-07-2012 at 10:52 AM..
This User Gave Thanks to ctsgnb For This Post:
# 7  
Old 02-08-2012
Hi ctsgnb

its not working ($2 in a) is searching whole pattern in fileB. but that pattern can be present in between of the line of fileB .

my files are as follows

fileA sepearated by tab /t
00 lieferungen
00 attractiop
01 done
02 forness
03 rasp
04 alwaysisng
04 funny
05 done1

fileB
funnymou120112
funnymou234470
raspmou3nhdhv
raspmou438748

so all those record which are greater than 3 and which are not present in fileB are to be redirected to third file.
eg : as in above file three records
03 rasp
04 alwaysisng
04 funny
05 done1
are greter than 3 . so rasp is compared to in each line of fileB . as rasp is present in any line of fileB .it is not redirrected in output file .. if rasp is not present in any line of fileB then it is redirected to output file . so output file for above will look like

o/p
04 alwaysisng
05 done1

plz help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files and extract the data which is present in other file - awk is not working

file2 content f1file2 content f1,1,2,3,4,5 f1,2,4,6,8,10 f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; !($1 in a)' file2 file1output f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; ($1 in a)' file2 file1output nothing... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

2. UNIX for Beginners Questions & Answers

Compare between two files with condition

Hello there. I am trying to compare two files. File1 Austria Mobile 1 United Kingdom Mobile 1 ... File2 Austria Mobile Vien 2 Austria Mobile Ostr 0 United Kingdom Mobile Dev 0.7 United Kingdom Mobile OST 1.5 What i want to do is to compare both files and... (12 Replies)
Discussion started by: dragonfly85
12 Replies

3. Shell Programming and Scripting

Compare two files and write data to second file using awk

Hi Guys, I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data. Example: Delimited File -------------- Byer;Amy;NONE1;A5218257;E5218257 Byer;Amy;NONE1;A5218260;E5218260 Positional File... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

4. Shell Programming and Scripting

Compare two numbers which present in two different files

Hi, I need to compare the two floating or integer numbers which present in two different files, Ex: File 1: col1 col2 col3 11 ssa 13.60 12 ssb 11.00 13 ssc 754.00 File 2:col1 col2 col3 11 sa 12.75 12 sb 11.00 13 sc 763.00 here i have to compare 3 column if 1st column match,... (8 Replies)
Discussion started by: Shenbaga.d
8 Replies

5. Shell Programming and Scripting

compare 2 files and return unique lines in each file (based on condition)

hi my problem is little complicated one. i have 2 files which appear like this file 1 abbsss:aa:22:34:as akl abc 1234 mkilll:as:ss:23:qs asc abc 0987 mlopii:cd:wq:24:as asd abc 7866 file2 lkoaa:as:24:32:sa alk abc 3245 lkmo:as:34:43:qs qsa abc 0987 kloia:ds:45:56:sa acq abc 7805 i... (5 Replies)
Discussion started by: anurupa777
5 Replies

6. Shell Programming and Scripting

How to compare data from 2 zip files and capture the new records from file2 to a new file

I have 2 zip files which have about 20 million records in each file. file 2 will have additional records than file 1. I want to compare the records in both the files and capture the new records from file 2 into another file file3. Please help me with a command/script which provides me the desired... (8 Replies)
Discussion started by: koneru
8 Replies

7. Shell Programming and Scripting

Compare columns of 2 files based on condition defined in a different file

I have a control file which tells me which are the fields in the files I need to compare and based on the values I need to print the exact value if key =Y and output is Y , or if output is Y/N then I need to print only Y if it matches or N if it does not match and if output =N , then skip the feild... (7 Replies)
Discussion started by: newtoawk
7 Replies

8. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

9. UNIX for Dummies Questions & Answers

how do i compare and extract similiar data

I have 2 files. The first file contains user names in one column. The second, and considerably longer, file contains user names in the first column and corresponding full names in the second column. Currently these are in the .xls format. I'd like to be able to compare file1 with file2 and extract... (2 Replies)
Discussion started by: raptrmastr
2 Replies

10. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question