Grep a file pattern in another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep a file pattern in another
# 1  
Old 10-08-2013
Grep a file pattern in another

Hi

I'm new to the forum, so I'd apologize for any error in the format of the post.

I'm trying to find a file content in another one using:

Code:
grep -w -f file1 file2

file1
Code:
GJA7
TSC

file 2
Code:
GJC1	GJA7
TSC1	TSC
TSC22D3	TSC-22R

so I expect the result to be:

Code:
GJC1	GJA7
TSC1	TSC

but instead it's:

Code:
GJC1	GJA7
TSC1	TSC
TSC22D3	TSC-22R

actually the problem is that it sees TSC-22R like two words, not one, and so matches the first part it with TSC.


Thanks

Last edited by flyfisherman; 10-08-2013 at 02:07 PM..
# 2  
Old 10-08-2013
Try

Code:
$ cat a
GJA7
TSC

Code:
$ cat b
GJC1    GJA7
TSC1    TSC
TSC22D3    TSC-22R

Code:
$ awk 'NR == FNR {_[$0]++; next} !($0 in _) && length($1)<5' a b
GJC1    GJA7
TSC1    TSC

# 3  
Old 10-08-2013
Try:
Code:
awk 'NR==FNR{A[$1]; next} {for(i=1; i<=NF; i++) if ($i in A) {print; next}}' file1 file2


Last edited by Scrutinizer; 10-08-2013 at 04:01 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 10-08-2013
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk 'NR==FNR{A[$1]; next} {for(i=1; i<=NF; i++) if ($i in A) {print; next}}' file

you missed 2nd file I think while postingSmilie

I think second if ($i in A) {print; next}} is not necessary

Scrutinizer do you have solution for this thread using grepI am interested

Last edited by Akshay Hegde; 10-08-2013 at 03:27 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 10-08-2013
That is right, thanks I corrected it in my post, thanks. The {print; next} is there, otherwise a line would get printed twice if there is a double match, for example..

Last edited by Scrutinizer; 10-08-2013 at 04:12 PM..
# 6  
Old 10-08-2013
Quote:
Originally Posted by Scrutinizer
That is right, thanks I corrected it in my post, thanks. The {print; next} is there, otherwise a line would get printed twice if there is a double match, for example..
I was not aware about it...Thank you..so much Scrutinizer
# 7  
Old 10-08-2013
Quote:
Originally Posted by Akshay Hegde
[..]
Scrutinizer do you have solution for this thread using grepI am interested
A grep solution might be (if your grep understands "-"):
Code:
sed 's/^/(^|[[:space:]])/; s/$/([[:space:]]|$)/' file1 | grep -Ef - file2

or (in bash / ksh93 )

Code:
grep -Ef <(sed 's/^/(^|[[:space:]])/; s/$/([[:space:]]|$)/' file1 ) file2

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

2. UNIX for Dummies Questions & Answers

Problem with grep from pattern file with if

I have two sets of data accept.txt is the list of 50 words Tom Anne James ... and I have a file01.txt which has the frequency of words which I got it using cat main.file | tr -d '' | tr ' ' '\n' | tr 'A-Z' 'a-z' | sort | uniq -c | sort -n -r > file01.txt so my file now looks like this... (2 Replies)
Discussion started by: A-V
2 Replies

3. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

4. Shell Programming and Scripting

Issue with grep using pattern from file

I'm trying to read in a list of text strings from a file, then use that string as a grep pattern. I am getting partial matching, which I believe stems from the linefeed at the end of each line of text. What I have now is: while read line; do echo $line; grep -i $line... (1 Reply)
Discussion started by: rogowar
1 Replies

5. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

6. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

7. Shell Programming and Scripting

Want to grep exact pattern from file

Contents of my file is: DI DI DIR PHI I want to extract only DI. I am using below command grep -w DI <file> but it is also extracting DI. Can i use any other command to extract exact pattern where '[' does not have special meaning (4 Replies)
Discussion started by: nehashine
4 Replies

8. UNIX for Dummies Questions & Answers

Grep in a file for a particular pattern in a particular position witihn the file

Assume I have a file with a lot of data sets like 123 abc 01 456 def 02 789 ghi and I only want to grep all that datasets from my file having the pattern '02' at the postion 9-10 to get only 456 def 02 So I could group the datsets into three files according to the position 9-10, one... (9 Replies)
Discussion started by: ABE2202
9 Replies

9. Shell Programming and Scripting

grep pattern in a file not working. please help...

Guys, I have my.mrk file as follows: rs112 rs105 rs154 rs136 ... and my.map file: 7 rs112 0.59 7 rs188 0.63 7 rs105 0.77 7 rs113 0.84 7 rs154 0.92 7 rs111 1.46 7 rs095 1.71 (3 Replies)
Discussion started by: Zoho
3 Replies

10. UNIX for Dummies Questions & Answers

Grep a pattern in gz file

I have a set of .gz files. I need to grep a pattern and need to find out the file in which that pattern occurs. zgrep in not available in my server.Any other options available for searching a pattern without unzipping the .gz files. (2 Replies)
Discussion started by: rprajendran
2 Replies
Login or Register to Ask a Question