grep -w Search for the expression as a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep -w Search for the expression as a word
# 1  
Old 04-08-2008
grep -w Search for the expression as a word

I have a file (file1) with around 5000 records and another file (file2) with 50000 records.
I want to search each word in file 1 from file 2 and spew the output of the matches in file3.

Can someone please help me here.

I tried doing this in ksh

for i in `cat file1`
do
grep -w $i file2 >> file3
done

this doesn't seem to work

Sample
Contents of file1
----------------
dll3ma1.10
dll3ma1.10.1
dll3ma1.10.1.0
dll3ma1.10.1.0.142

Sample Contents of file 2
------------------------
dll3ma1.10
dll3ma1.10.4
dll3ma1.10.4.0
dll3ma1.10.4.0.140
dll3ma1.10.4.0.158
dll3ma1.10.4.0.144
dll3ma1.10.2
dll3ma1.10.2.0
dll3ma1.10.2.0.119
dll3ma1.10.4.0.141
dll3ma1.10.4.0.102
dll3ma1.10.4.0.145
dll3ma1.10.1
dll3ma1.10.3
dll3ma1.10.3.0
dll3ma1.10.3.0.121


The output I am expect in file3 should be
dll3ma1.10
dll3ma1.10.1
# 2  
Old 04-08-2008
Code:
awk 'FNR==NR{
 a[$1]
 next
}
{
  if ( $1 in a ) print $1
}' file file1

# 3  
Old 04-08-2008
/usr/local/bin/awk 'FNR==NR{ a[$1] next } { if ( $1 in a ) print $1 }' file1 file2
awk: cmd. line:1: FNR==NR{ a[$1] next } { if ( $1 in a ) print $1 }
awk: cmd. line:1: ^ parse error


Here file1 has the contents that I am looking for in file2
# 4  
Old 04-08-2008
use nawk or gawk if you have it.
# 5  
Old 04-08-2008
Same results

/usr/local/bin/gawk 'FNR==NR{ a[$1] next } { if ( $1 in a ) print $1 }' file1 file2
gawk: cmd. line:1: FNR==NR{ a[$1] next } { if ( $1 in a ) print $1 }
gawk: cmd. line:1: ^ parse error


/usr/bin/nawk 'FNR==NR{ a[$1] next } { if ( $1 in a ) print $1 }' file1 file2
/usr/bin/nawk: syntax error at source line 1
context is
FNR==NR{ a[$1] >>> next <<< } { if ( $1 in a ) print $1 }
/usr/bin/nawk: illegal statement at source line 1
# 6  
Old 04-08-2008
Quote:
Originally Posted by knijjar
Same results

/usr/local/bin/gawk 'FNR==NR{ a[$1] next } { if ( $1 in a ) print $1 }' file1 file2
gawk: cmd. line:1: FNR==NR{ a[$1] next } { if ( $1 in a ) print $1 }
gawk: cmd. line:1: ^ parse error


/usr/bin/nawk 'FNR==NR{ a[$1] next } { if ( $1 in a ) print $1 }' file1 file2
/usr/bin/nawk: syntax error at source line 1
context is
FNR==NR{ a[$1] >>> next <<< } { if ( $1 in a ) print $1 }
/usr/bin/nawk: illegal statement at source line 1
try not putting them on one line!!
# 7  
Old 04-08-2008
I believe the OP need only files compared line by line:
Code:
egrep -f file2 file1

my 2cents
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

3. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

4. Linux

How to search multiple word using grep command?

How to search multiple word using grep command for example i want to reserch ANJ001 AA Using ridiculous font, size, and color changes instead of normal space separated text and CODE tags obfuscates what you are trying to do and makes it difficult for volunteers who may want to help you solve... (1 Reply)
Discussion started by: na.dharma
1 Replies

5. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

6. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

7. Shell Programming and Scripting

Grep Search for both First and last word

Hi Pretty sure this is very simple hence im banging my head o the wall as to why i cant get this to work: Im greping against many files where there will be the following string: Date: Thu, Aug 23 2001 09:27 PM (of course values such as date ,time and PM/AM will vary.) Im trying to grep... (1 Reply)
Discussion started by: duonut
1 Replies

8. Shell Programming and Scripting

Grep word search

Hi, I have grep command to search for a word "SUB" as below, grep -w "SUB" file1 But this is taking a word like SUB.XXY or SUB.BBB etc which is not required.. I need only if it is a word "SUB". Any suggestions ??? Thanks, Vasanth. (11 Replies)
Discussion started by: skcvasanth
11 Replies

9. Shell Programming and Scripting

Perl Regular Expression Word Search

I'm trying to have my perl script telnet into the network device execute a command then dump the output of the command into a variable. The script then greps for the word "STANDBY". I can't seem to get the script to print out the output because it seems that the script can't find the word... (1 Reply)
Discussion started by: xmaverick
1 Replies

10. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 Replies
Login or Register to Ask a Question