Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have same problem with the comparing 2 files .
My requirement is ...
cat 1234
1
2
3
4
5
6
67
7
............
cat file1
1
3
5
67
...........................
Now I want "file1" to compare with "1234"...
1 is present in both the file so it should be printed
but 7 is not there in the "file1" so it should not be printed.
If you want common lines printed, look at the "comm" command. The two files need to be sorted.
comm -12 <(sort file1) <(sort file2)
The command normally prints out 3 columns; unique to the first file, unique to the second, common to both. The -12 options suppresses the first 2 columns of output leaving just the lines common to both.
If you want common lines printed, look at the "comm" command. The two files need to be sorted.
comm -12 <(sort file1) <(sort file2)
The command normally prints out 3 columns; unique to the first file, unique to the second, common to both. The -12 options suppresses the first 2 columns of output leaving just the lines common to both.
thanks for immediate reply.
actually I have one file with 500 host names and other file with list of 200 host names.
I want to find whether the 2nd set of 200 host names are present in the 1st file of 500 host name....
when I trying to use the command which u get gave....it's showing me the error msg " file 2 not in sorted order"......."file 1 not in sorted order"
i=0
while read line ; do
output=`echo $line`
if [[ -n "$output" ]]
then
myarray[$i]=$output
i=`expr $i + 1`
fi
done < "1234"
while read line ; do
match=0
dat=`echo "$line"`
if [ -n "$dat" ]
then
for (( x=0 ; x < ${#myarray[@]} ; x++ )) do
if [ "${myarray[$x]}" == "$dat" ]
then
match=1
echo ${myarray[$x]}
fi
done
fi
if [ $match = 0 ]
then
echo "no match"
fi
done < "file1"
I believe that should accomplish what you want
Last edited by montel; 07-09-2012 at 01:25 PM.
Reason: Forgot array counter
thanks for immediate reply.
actually I have one file with 500 host names and other file with list of 200 host names.
I want to find whether the 2nd set of 200 host names are present in the 1st file of 500 host name....
when I trying to use the command which u get gave....it's showing me the error msg " file 2 not in sorted order"......."file 1 not in sorted order"
Hello
I must sort first , as said before :
Code:
sort 1234 > fa ; sort file1 > fb ; comm -12 fa fb > out ; nano out
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.