Find records between two files which are not exists in one another in one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find records between two files which are not exists in one another in one
# 1  
Old 04-26-2011
Find records between two files which are not exists in one another in one

Hello all,

Would like to know how to find records between two files which are not exists in one another in one. For example: I've two files "fileA" and "fileB" and want to find record from "fileB" which does not exists in "fileA".

fileA
--------
ABCD
DEFG
GHIJ
KLMN
NOPQ
RSTU
VUWX
XYZA

fileB
--------
ZYXW
ABCD
VUVS
RQPO
NMLK
JIHG
FEDC
BAZY
KLMN
RSTU

From above, the expected result must be,

fileC
--------
ZYXW
VUTS
RQPO
NMLK
JIHG
FEDC
BAZY


Please advise how to handle such cases from command line.

Thanks,
Venkat
# 2  
Old 04-26-2011
Finds records from fileB which are not in fileA:
Code:
awk 'NR==FNR{a[$0]; next}!($0 in a)' fileA fileB

---------- Post updated at 06:35 AM ---------- Previous update was at 06:34 AM ----------

SmilieI have post code of this like for more than once...
# 3  
Old 04-26-2011
Quote:
Originally Posted by kevintse
SmilieI have post code of this like for more than once...
Smilie

You could also use grep if your grep version supports the -f option:
Code:
grep -v -f fileA fileB

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 04-26-2011
Quote:
Originally Posted by Franklin52
Smilie
You could also use grep if your grep version supports the -f option:
Code:
grep -v -f fileA fileB

I really like this one Smilie
# 5  
Old 04-26-2011
You could also use the comm tool. See the comm man page.

Regards,
Mark.
# 6  
Old 04-26-2011
Quote:
Originally Posted by Franklin52
Smilie

You could also use grep if your grep version supports the -f option:
Code:
grep -v -f fileA fileB

This really looks cool, but it can be much slower than the awk solution if fileA is large since grep has to take each line in fileA as a Regex, and greps against the whole file of fileB.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find out whether a file exists with the help of regular expression?

Hi all I have a list of file names in array. But this file names are not exact. so i want to search whether a file exists or not using regular expression. code snippet: if ; then echo "File exists" else echo "File does not exits" fi over here "*EQST*" should be treated as a regular... (4 Replies)
Discussion started by: Ganesh_more
4 Replies

2. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

3. UNIX for Dummies Questions & Answers

Statement to find if an entry exists in a file

I need to check if an entry input by the user is in a file. If so, I need to run a command, and if it does not exist then it should output entry does not exist. So I have so far... echo "Enter record:" read record //command || //command Can I use an if statement to do this? (3 Replies)
Discussion started by: itech4814
3 Replies

4. Shell Programming and Scripting

how to find these records.

Hi, Unix Gurus, I need find records in a file which first 5 character is empty. eg. 12344567 233 467 345 435 456 2334455555 4t54 eee 233445555 444 aaa eerer eree ereei want to find 345 435 456 eerer eree eree:wall: Thanks in advance (2 Replies)
Discussion started by: ken002
2 Replies

5. UNIX for Advanced & Expert Users

Find out records in between 50 to 60

my file contain 1 to 100 numbers and i want to display 50 to 60 numbers in a seperate file. how many ways this is possible in unix:confused: (3 Replies)
Discussion started by: mailkrissh
3 Replies

6. Solaris

Find a whether user exists or not.

Hi all, to find a user whether he had an account on AIX box i will use commands like "finger" , "lsuser". I am new to solaris and we are migrating to solaris. now i am using " more /etc/passwd | grep -i <UserID> " to find a user present in that solaris box or not. Are der any similar... (9 Replies)
Discussion started by: firestar
9 Replies

7. Shell Programming and Scripting

find, if exists then append for file with same name

I will have to process multiple files with same name everyday. My requirement is: If on a certain day I see that filename.txt exists then the contents of the filename.txt would be added/append to the former file contents.Each time it sees the file the content would be added.But the header ... (8 Replies)
Discussion started by: RubinPat
8 Replies

8. Shell Programming and Scripting

unix script to check whether particular file exists and to find its size

I want to find the size of particular file exists in a particular directory and i wnt to zip it. In the below mentioned code it should check the MQ.log in the particular directory.Please correct my code so that it will check for particular MQ.log but i could not able to check whether the... (9 Replies)
Discussion started by: Balachandar
9 Replies

9. UNIX for Dummies Questions & Answers

Help comparing 2 files to find deleted records

Hi, I need to compare todays file to yesterdays file to find deletes. I cannot use comm -23 file.old file.new. Because each record may have a small change in it but is not really a delete. I have two delimited files. the first field in each file is static. All other fields may change. I... (2 Replies)
Discussion started by: eja
2 Replies

10. Shell Programming and Scripting

-s option to find object exists not working.

is there a direct command to find whether directory is empty, -s option doesn't seem to work. Mark. (2 Replies)
Discussion started by: markjason
2 Replies
Login or Register to Ask a Question