|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Try grep with the -f option:
Man Page for grep (POSIX Section 1) - The UNIX and Linux Forums Regards |
|
#3
|
|||
|
|||
|
Code:
uniq -d file1 file2 |
|
#4
|
|||
|
|||
|
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
|
||||
|
||||
|
Code:
awk 'FNR==NR{a[$1];next} $1 in a}' file1 file2 |
|
#6
|
|||
|
|||
|
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 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
|
|||
|
|||
|
you can use comm or diff command, refer this example: Compare Two Files Using Comm
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| 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 |