find common elements in 2 files (for loop) | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

find common elements in 2 files (for loop)

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-28-2007
ibking ibking is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 29 October 2007, 8:49 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Bug find common elements in 2 files (for loop)

Hi,
i'm new here (and to scripting too).

I was hoping for some help in comparing two files.

i have a file called 'file1' with a list of names in the following format:
adam jones
paul higgins
kelly lowe


i also have another file which may contain some of the names but with a lot of other stuff, this is called 'file2'. it contains info such as:
aaa aaaa paul higgins aaaaa
aaaaa aaaaa aaaa aa aa aaa
aaaa aaaa kelly lowe aaaaa
asdsaad adsadsa asdas
assadasd
sa etc

(where aaaa is just random stuff html)

i want to find the common names in both files and produce an output in the form of:
0
1
1


this is with reference to the 'master' file (file1). ie. it has found in the 'file2' file that there are no adam jones, and 1 each of paul and kelly.

to do this task, i am using the following shell script however it does not seem to work:

#!/bin/sh

for i in file1

do

grep -c $i file2

done


it seems like quite a simple script, but i'm not sure why it does not work?(it only seems to grep for the first name, but does not loop through)

thanks for any help
Sponsored Links
    #2  
Old 10-28-2007
Lakris Lakris is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 13 June 2012, 2:08 PM EDT
Location: Stockholm
Posts: 317
Thanks: 0
Thanked 7 Times in 7 Posts
Your code use "file1" as argument, not its content.
Try:

Quote:
while read line; do
grep -c "$line" file2;
done < file1
You will need to protect spaces in $line with quotation marks.
Sponsored Links
    #3  
Old 10-29-2007
ibking ibking is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 29 October 2007, 8:49 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
thanks!
that worked perfectly
    #4  
Old 12-11-2007
ahjiefreak ahjiefreak is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 11 December 2008, 9:39 AM EST
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
How to read content from a file and match with another file for operation

Hi,

I have somehow similar case of question like the above with some differences.

Basically I have 2 files, file A and file B.

In file A, I have columns of fields such that:-

aaa 107
bbb 108
ccc 109

In file B, I have columns of fields such that:-
101 2 1
102 3 1
107 2 1
108 3 1
109 2 1

I would like to know, if I would like to extract let say first element of file A and compare with file B elements.

If found, I would like to have the position of element 107 in file B in this case it is on 3rd line. And the same goes for other elements in file A which in the end will pipe to another output file.

Next, I would like to get the $2 and $3 of the 3rd line for some computation.

Currently, I tried to do below:-

#!/bin/sh

for i in a.txt

do

grep -c $i b.txt

cat b.txt| awk '
{
count[$i]=$2+$3;

}
END{
printf("%d",count[$i]);
}'
done

I knew it is not going work well as the second field of A.txt is not extracted.

Please advise. Thanks.


-Jason
Sponsored Links
    #5  
Old 12-11-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 22 October 2014, 2:57 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,683
Thanks: 42
Thanked 197 Times in 179 Posts
Hi, ahjiefreak.

It's good that you posted your scripting effort. However:

1) new problems posted in old threads often get over-looked, and some people consider it rude,

2) you've seen requests to put code tags around scripts and data,

3) you didn't post the expected output,

4) it looks like homework

Best wishes ... cheers, drl
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
Writing a loop to merge multiple files by common column evelibertine UNIX for Dummies Questions & Answers 1 10-07-2011 11:20 AM
how to find common words and take them out from two files kaixinsjtu UNIX for Dummies Questions & Answers 11 04-01-2009 09:51 AM
Files common in two sets ??? How to find ?? skyineyes Shell Programming and Scripting 1 12-15-2008 04:19 AM
To find all common lines from 'n' no. of files The Observer Shell Programming and Scripting 11 06-16-2008 02:22 PM



All times are GMT -4. The time now is 09:39 PM.