grep exact string from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep exact string from a file
# 1  
Old 11-02-2011
grep exact string from a file

Hi,

I have the following output from a file
Code:
zone "adm.test.com" {
abc
test1.db
}
zone "test.com" {
xyz
test2.db
}
zone "1.test.com.sg" {
1abc
test3.db
}
zone "3.test.com.uk" {
1xyz
test4.db
}

I need to grep for "test.com" only, count the lines number and then add 2 to the line number so as to obtain "test2.db".

I tried "grep -n -w test.com" and it greps for multiple instances. How do I grep only exact matches and then do the line count? I am running the script on Solaris 10 OS.

Pls. help. Thanks.


Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by zaxxon; 11-03-2011 at 03:25 AM.. Reason: code tags, see PM
# 2  
Old 11-02-2011
I'd use awk rather than to try to make grep work out of its comfort zone.

Code:
awk '
    /zone "test.com"/ {
        getline;
        getline;
        print; exit(0);
    }
'

You can smash it all on one line and have it still be readable:
Code:
awk '/zone "test.com"/ { getline; getline; print; exit(0);}'

# 3  
Old 11-03-2011
grep -w is the right option to find the exact string
# 4  
Old 11-03-2011
Through Sed..
Code:
sed -n '/\"test.com\"/{n;n;p}' inputfile

# 5  
Old 11-03-2011
grep -w will do the work

input text:
Code:
hi how are you doing?
you people are great
your frnd just called me
this pen is yours right? are you sure

and when i grepped it..

Code:
# grep -w 'your' inputfile
your frnd just called me

when you used -w grep looked exactly for "your" and it didnt select the word "yours" from the file..which would have been selected if you had not used -w option
# 6  
Old 11-03-2011
Quote:
Originally Posted by vchee
I am running the script on Solaris 10 OS.

On SunOS this should also work..

Code:
grep -A2 '"test.com"' file | tail -1

# 7  
Old 11-08-2011
Thanks for all the help. Works fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for an exact match in a file

I am currently having some issues while trying to grep for a exact string inside a file. I have tried doing this from command line and things work fine i.e. when no match is found, return code=1 but when its done as part of my script it returns 0 for the same command - I dont know if there is an... (6 Replies)
Discussion started by: Ads89
6 Replies

2. Shell Programming and Scripting

Grep to find an exact string

Hi all, I tried searching the forum for this, and I read numerous suggestions here and even on other forums, and I cannot get this to want the way that I need it to. I tried grep -W / -f to no luck. Here is what I have. I have a list of file names- FILE1-FILE1TEST,FILE1RELATION... (7 Replies)
Discussion started by: jeffs42885
7 Replies

3. UNIX for Dummies Questions & Answers

How to grep exact string with quotes and variable?

As the title says I'm running a korn script in attempts to find an exact match in named.conf finddomain.ksh #!/bin/ksh # echo "********** named.conf ************" file=/var/named/named.conf for domain in `cat $1` do grep -n '"\$domain "' $file done echo "********** thezah.inc... (1 Reply)
Discussion started by: djzah
1 Replies

4. Shell Programming and Scripting

Grep exact string from main string

Hi , am getting output file, it sontains the below values. ./hawk_DOM1_FIRST_ENV ./hawk_DOM2_SECOND_ENV ./hawk_DOM3_THIRD_ENV Now I need to grep the word "DOM1_FIRST_ENV","DOM2_SECOND_ENV" like that. I tired with cut -d "_". Its not working with any deleimiter. Can you please help to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

5. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

6. UNIX for Dummies Questions & Answers

How to grep the exact string / word ?

Hi All, I have a text / log file which contains strings like meta777, 77, meta, 777. Now I want to write a script which can detect a string 'meta#777' in a text file & number of occurence of 'meta', number of #, number 7, 77, 777. I'm using grep -e '77' filename but no luck. It is returning... (5 Replies)
Discussion started by: adc22
5 Replies

7. 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

8. Shell Programming and Scripting

How to use grep to get an exact string?

Hi there, I've search this forum and find this problem could have been solved by, grep -ho "num=*" input_data The input_data is, 1\11\num1=100\num2=200\newnum1=220\\@ however, what I have got is , num1=100 num1=220 how to get the exact string, (4 Replies)
Discussion started by: liuzhencc
4 Replies

9. 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 but..it'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

10. UNIX for Dummies Questions & Answers

grep exact string/ avoid substring search

Hi All, I have 2 programs running by the following names: a_testloop.sh testloop.sh I read these programs names from a file and store each of them into a variable called $program. On the completion of the above programs i should send an email. When i use grep with ps to see if any of... (3 Replies)
Discussion started by: albertashish
3 Replies
Login or Register to Ask a Question