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
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
Try grep with the -f option:
Man Page for grep (POSIX Section 1) - The UNIX and Linux Forums
Regards
uniq -d file1 file2
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
awk 'FNR==NR{a[$1];next} $1 in a}' file1 file2
If both files are sorted, you can simply use the comm utility:
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
sort -n filea fileb | uniq -d
Regards,
Alister
Hi.
This method may report false positives in some circumstances. For example:
#!/usr/bin/env bash
# @(#) s1 Demonstrate false positive for replicated items.
# Infrastructure details, environment, commands for forum posts.
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p specimen sort uniq
set -o nounset
# If not available, replace "specimen" with "more data?|cat"
echo
specimen data1 data2
echo " Results, expecting \"g\", not really common:"
sort data1 data2 |
uniq -d
exit 0
producing:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0
GNU bash 3.2.39
specimen (local) 1.3
sort (GNU coreutils) 6.10
uniq (GNU coreutils) 6.10
Whole: 3 of 3 lines in data1
g
a
g
Whole: 3 of 3 lines in data2
a
m
b
Results, expecting "g", not really common:
a
g
cheers, drl
grep -f file2 file1
Hi, drl. You are quite correct. Nice catch and thank you for bringing it to my attention. I will append a warning to my earlier post momentarily.
Regards,
Alister