Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
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: 5,591
Thanks: 2
Thanked 68 Times in 65 Posts
Try grep with the -f option:

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

Regards
  #3  
Old 03-14-2010
Registered User
 

Join Date: Mar 2009
Location: france, montpellier
Posts: 164
Thanks: 0
Thanked 0 Times in 0 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
  #5  
Old 03-14-2010
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,611
Thanks: 2
Thanked 36 Times in 33 Posts

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

  #6  
Old 03-14-2010
Registered User
 

Join Date: Dec 2009
Posts: 660
Thanks: 6
Thanked 49 Times in 47 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
  #7  
Old 03-14-2010
thegeek thegeek is offline Forum Advisor  
Registered User
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 870
Thanks: 2
Thanked 10 Times in 8 Posts
you can use comm or diff command, refer this example: Compare Two Files Using Comm
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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 07:59 AM.