Select the exact matching contents using grep

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select the exact matching contents using grep
# 1  
Old 06-08-2011
Bug Select the exact matching contents using grep

Hi everyone I've two files..
The contents of file1 are as shown below


The contents of file2 are as shown below

21 hello
13 world
4 Hi
36 how
44 are
5 you
56 ?

If I use

grep -f file1 file2

I get the following output

13 world
4 Hi
44 are
5 you
56 ?

I want output as shown below

13 world
4 Hi
5 you

I've to match the exact line number at the beginning of the line!!
How can I do it??
I need to write a shell script for that..
# 2  
Old 06-08-2011
try this...
grep -Fw file1 file2

# 3  
Old 06-08-2011
awk 'FNR==NR{a[$1]=1} NR>FNR{if($1 in a)print $0}' f1 f2

# 4  
Old 06-08-2011
nawk 'FNR==NR{f1[$0];next} $1 in f1' file1 file2

# 5  
Old 06-08-2011
thanks for the reply
but am not getting any output by using grep -Fw command..Smilie

---------- Post updated at 02:49 PM ---------- Previous update was at 02:42 PM ----------

thank u..its working using awk command..Smilie
# 6  
Old 06-08-2011
Using solaris you can try this syntax hope this should work...
/usr/xpg4/bin/grep  -w -f file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - Exact pattern matching and replace

Hi Team, I am facing a problem as under, Suppose I have a file (test.txt) with the below content (all braces and slashes are included in the contents of the file) Now I want to append few words below matched line, I have written the below sed: sed '/option/a insert text here' test... (2 Replies)
Discussion started by: ankur328
2 Replies

2. Shell Programming and Scripting

Convert text between exact matching patterns to Title case

Hi Folks, I have a large text file with multiple similar patterns on each line like: blank">PATTERN1 some word PATTERN2 title=">PATTERN1 some word PATTERN2 blank">PATTERN1 another word PATTERN2 title=">PATTERN1 another word PATTERN2 blank">PATTERN1 one more time PATTERN2 title=">PATTERN1... (10 Replies)
Discussion started by: martinsmith
10 Replies

3. Shell Programming and Scripting

Select command with variable options having spaces or null contents

Hi, I'm having an issue trying to produce a hierarchical directory menu that has either any directories listed in a specific directory as options or options with spaces in the option content or null content. So the menu function gets passed a base directory, it then lists any .sh scripts in... (6 Replies)
Discussion started by: andyatit
6 Replies

4. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

5. Shell Programming and Scripting

How do i select all contents between two numbers?

I want to select contents between two numbers say 1. and 2. from an output file which has many numbers but only the these two ending with a dot(.) eg 1. 2 . 32. etc I was looking to select with the use of a counter then modify the selected contents and put it in an output file where again the... (3 Replies)
Discussion started by: ausfragen
3 Replies

6. Shell Programming and Scripting

sed to exact matching a word in free BSD

Hi, Thanks for looking at this issue. I have many words/lines in a files like below apple pine apple custored apple apple apple if i want to replace only/exact apple occurrences with XXX i could use below, sed 's/\<apple\>/XXX/g' filename this is working in Linux, but now in my free BSD... (3 Replies)
Discussion started by: ramanaraoeee
3 Replies

7. UNIX for Dummies Questions & Answers

Matching exact string with blank space using grep

hi! i'm trying to get grep to do an exact match for the following pattern's not quite working. I'm not too sure where did I get it wrong. any input is appreciated. echo "$VAR" | grep -q '^test:]name' if ; then printf "test name is not found \n" fi on... (4 Replies)
Discussion started by: jazzaddict
4 Replies

8. Shell Programming and Scripting

select contents between two delimiters (not working if newline in encountered)

Hi, I am facing difficulties in selecting the contents between two delimiters when there is a new line occurs.. Eg: >more sample.txt abcd -- this is the first line % efgh-- this is the second line and not able to print % ijkl -- this is the 3rd line % when i search for abcd and... (8 Replies)
Discussion started by: Balaji PK
8 Replies

9. Shell Programming and Scripting

matching 2 exact fields

Dear experts, I have a file1 that looks like 60127930928 2091 60129382039 2092 60126382937 2091 60128937928 2061 60127329389 2062 60123748730 2061 60128730293 2061 and file 2 that looks like 60127930928 2091 60129382039 2092 60126382937 2093 60128937928 2061 60127329389... (2 Replies)
Discussion started by: aismann
2 Replies

10. UNIX for Dummies Questions & Answers

Select text within matching ( ) bracket

Hi, I am looking for a simple command to select text within a open bracket "(" and a matching close bracket ")" and output the within-bracket-text to a file. This function is similar to the common vi select a range of text with "(" to ")" but not sure how to run the same function in command... (4 Replies)
Discussion started by: cursive
4 Replies
Login or Register to Ask a Question