Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-14-2010
Banned
 

Join Date: Apr 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
common contents of two files

I have two files: file a with contents
1
2
3
4
5

file b with contents

6
3
5
8
9
10

i want go get file c which has the common contents of both files
so file c should have contents
3
5


Thank you
Sponsored Links
    #2  
Old 03-14-2010
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 7,289
Thanks: 55
Thanked 427 Times in 408 Posts
Try grep with the -f option:

Man Page for grep (POSIX Section 1) - The UNIX and Linux Forums

Regards
Sponsored Links
    #3  
Old 03-14-2010
Registered User
 

Join Date: Mar 2009
Location: france, montpellier
Posts: 170
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
uniq -d file1 file2

    #4  
Old 03-14-2010
Banned
 

Join Date: Apr 2008
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
unid -d file1 file1 is not working,i get file2 being made empty

---------- Post updated at 03:28 PM ---------- Previous update was at 03:12 PM ----------

is there any way i can use awk to do this
Sponsored Links
    #5  
Old 03-14-2010
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 6,959
Thanks: 69
Thanked 351 Times in 338 Posts

Code:
awk 'FNR==NR{a[$1];next} $1 in a}' file1 file2

Sponsored Links
    #6  
Old 03-14-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
If both files are sorted, you can simply use the comm utility:

Code:
comm -1 -2 filea fileb



---------- Post updated at 01:01 PM ---------- Previous update was at 12:55 PM ----------

Hello, protocomm

Quote:
Originally Posted by protocomm View Post
Code:
uniq -d file1 file2

That will simply always clobber file2 with one copy of repeated lines from file1 (if any, otherwise file2 will be empty). I think what you are going for is more along the lines of:
EDIT: Disregard the following pipeline. An item occuring twice in a file is indistinguishable from its occuring once in each file, creating the potential for false positives. Thank you drl for pointing it out (in a later post in this thread). -- Alister

Code:
sort -n filea fileb | uniq -d


Regards,
Alister

Last edited by alister; 03-15-2010 at 12:58 PM.. Reason: To flag error
Sponsored Links
    #7  
Old 03-14-2010
thegeek thegeek is offline Forum Advisor  
Registered User
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 1,090
Thanks: 13
Thanked 37 Times in 35 Posts
you can use comm or diff command, refer this example: Compare Two Files Using Comm
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting Common value in three files nuthalapati Shell Programming and Scripting 10 10-20-2009 02:09 PM
Merge files of differrent size with one field common in both files using awk shashi1982 Shell Programming and Scripting 2 03-03-2009 06:12 AM
Join file contents via common field Northerner Shell Programming and Scripting 8 01-29-2009 06:46 PM
list common name files mazhar99 UNIX for Dummies Questions & Answers 4 08-18-2008 11:21 PM
Get un common numbers from two files jingi1234 UNIX for Dummies Questions & Answers 3 10-19-2005 08:32 AM



All times are GMT -4. The time now is 03:49 AM.