Searching the content of one file using the search key of another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching the content of one file using the search key of another file
# 1  
Old 07-28-2014
Searching the content of one file using the search key of another file

I have two files:
file 1:
Code:
hello.com	neo.com,japan.com,example.com
news.net	xyz.com, telecom.net, highlands.net, software.com
example2.com	earth.net, abc.gov.uk

file 2:
Code:
neo.com
example.com
abc.gov.uk

file 2 are the search keys to search in file 1 if any of the search key is found in file 1 it should return the whole line of file 2 such that the desired output is:

Code:
hello.com	neo.com,japan.com,example.com
example2.com	earth.net, abc.gov.uk

I tried with the following awk script but yet I am not getting the desired output.
Code:
awk '{FS="\t"} NF==1 {key[$1]=1} NF > 1 {if( ( $2 in key ) ) {print $1 $2} }' file1 file2

Any suggestions to fix this ?
# 2  
Old 07-28-2014
A much simpler way to do this is:
Code:
grep -F -f file2 file1

Your awk script is failing because:
  1. key is empty when you're reading file1 (you need to switch the order of your operands so the keys will be read from file2 before you try to find the keys in file1, and
  2. your keys are single words (e.g., example.com), but $2 in file1 is a comma separated list of possible keys. The expression ( $2 in key ) will only be true if a single key from file2 is the only key in the list of keys in a line in file1.
# 3  
Old 07-29-2014
Thank you very much for the clear explanation about awk. The grep script works perfectly fine but just one another concern. If in case my output is just the same as above but with a slight change like this, if the search is found it has to return the whole line else just the first column
Code:
hello.com    neo.com,japan.com,example.com 
news.net     
example2.com    earth.net, abc.gov.uk

Any suggestions in this ?
# 4  
Old 07-29-2014
Try
Code:
awk -F'\t' 'NR==FNR {key[$1];next}  {printf "%s", $1; for (i in key) if ($2 ~ i) {printf "\t%s", $2; break}; printf "\n" }' file2 file1
hello.com    neo.com,japan.com,example.com
news.net
example2.com    earth.net, abc.gov.uk

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for specific key in a file and print it

Hi All, I have a file abc.txt Database unique name: NEPAL Database name: NEPAL Services: NEPAL_COB,NEPAL_PROD output i need is it should search "Services" and it that search "COB" word so output i need is like NEPAL_COB (3 Replies)
Discussion started by: amar1208
3 Replies

2. Shell Programming and Scripting

Search the key from a file to another file

Hi, I need help in solving the below need in UNIX. I have a file datafile as below pg1,dvn1,scls1,cls1,itp1,sku1 pg2,dvn2,scls2,cls2,itp2,sku2 and keyfile as below scls1,1000,a,b,c,d,p1 scls2,1001,a,b,c,d,p1 scls1,1000,a1,b,c,d,p1 scls2,2000,a,b,c,d,p1 (4 Replies)
Discussion started by: bhaski2012
4 Replies

3. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

4. UNIX for Dummies Questions & Answers

Using File Browser to search file content ...

Hello, I'm using rhel6. Using File Browser Nautilus 2.28.4 I could easily locate any file I'm interested in by it name. I'd like to use this File Browser to locate the file name based on it content e.g. based on some word in the text file. It doesn't work for me that way ... My question: does... (0 Replies)
Discussion started by: susja
0 Replies

5. Shell Programming and Scripting

Search Key in same file but at different positions

Hi All, I want some help in unix. Current code is for DOC in `InputFileName | awk '{ if ( substr($0, 392, 1)=="C") { $0 = (substr($0, 28, 15) "" substr($0, 386, 3)) } else { ($0 = substr($0, 28, 18))}; print }' | sort | uniq` do grep ${DOC} InputFileName > DOCKET_${DOC}.txt done ... (18 Replies)
Discussion started by: kam786sim
18 Replies

6. UNIX for Dummies Questions & Answers

Searching a file with exclusion in the search

Hello, I'm looking for a bit of help. Im trying to search a file for lines that contain white spaces at the end of the lines. This is what I'm using where $param is the path and file name and it redirects the output to a txt file : echo | grep -n ' $' $param >> $2 Is it possible to have... (8 Replies)
Discussion started by: gintreach
8 Replies

7. Linux

How to search file content

Hi folks, What will be an easy and effective way searching file content? E.G I need to find a WORD or a PHRASE on a file? TIA B.R. satimis (4 Replies)
Discussion started by: satimis
4 Replies

8. UNIX for Dummies Questions & Answers

Searching for key word within a file

Hello, I have a directory that holds all of my matlab codes. I am trying to run a searh on all of the matlab files that have the word "while" written inside the sytax of the code. Looking for all of the times that I did a while loop. Can someone help me do this? Thanks in advance. ... (1 Reply)
Discussion started by: moradwan
1 Replies

9. Shell Programming and Scripting

searching for filenames with search strings in another file

Hi, I have 5 files in a directory. emp1_usage.txt emp2_usage.txt emp3_usage.txt emp4_usage.txt emp5_usage.txt I am using sqlldr to get the contents of the above 5 files and store it in a temp table and update my original table using temp table. for f in *emp*.txt do sqlldr... (3 Replies)
Discussion started by: pathanjalireddy
3 Replies

10. Shell Programming and Scripting

Searching and Removing File Content

Hi, I am trying to search a character in a file and remove it from that file.... My file looks something like this: test1.txt ckj12300_00|123|var1|10.2 ckj00200_12|444|var2|11.2 ckj00200_14|4556|var3|33.5 c00200_00_000|4558|var4|33.5 ckj00200_14|4553|var5|33.5... (7 Replies)
Discussion started by: rkumar28
7 Replies
Login or Register to Ask a Question