grep exact string/ avoid substring search


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep exact string/ avoid substring search
# 1  
Old 11-03-2008
Error 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 these are running i cannot differentiate between the two.

I am using the following command:
ps -ef|grep $program|grep -v grep

The above command succeeds for both the cases even when only a_testloop.sh is running as testloop.sh is a part of a_testloop.sh. I want to grep/search only the exact string not substring. Please help.

Thanks,
Albert
# 2  
Old 11-03-2008
Use the -w option of grep.

Regards
# 3  
Old 11-03-2008
Tools

what about
1) grep for a_testloop will give you just that
2) grep for testloop and then grep -v a_testloop to exclude that one
# 4  
Old 11-03-2008
The ps-output is tabular, right?

You can search for whitespace too, if you protect them properly (otherwise the shell will eat them):

Code:
# cat myfile
 testloop
 a_testloop

# grep '[<spc><tab>]testloop' myfile
 testloop
# grep '[<spc><tab>]a_testloop' myfile
 a_testloop

Replace <spc> by a literal blank and <tab> by a literal tab character.

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Re: grep exact search

Hi, x=GATE ps -ef|grep pmon grid 552 1 0 Aug08 ? 00:00:51 asm_pmon_+ASM1 oracle 4314 1 0 Aug08 ? 00:04:08 ora_pmon_GATE1 oracle 5018 1351 0 10:14 pts/1 00:00:00 grep pmon oracle 7329 1 0 Aug08 ? 00:02:19 ora_pmon_NRID1 ps... (3 Replies)
Discussion started by: rcc50886
3 Replies

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

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

search and replace exact string

Hello Everyone, Im trying to run a search and replace of exact strings and the strings that im using variables that are passed through an array in a while loop. Here is a snip of my code: USEROLD=`cat oldusers` USERNEW=`cat newusers` USEROLDARRAY=( $USEROLD ) USERNEWARRAY=( $USERNEW )... (4 Replies)
Discussion started by: skizim
4 Replies

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

7. UNIX for Advanced & Expert Users

Search for an exact string in a Terminal

Is there hopefully a way to search for an exact string in Man Pages? I know if I want to search for anything containing -c I can just do this. /-c How would I search for "-c"? I want only "-c" to show up. So I tried this. /"-c" It took me literally and looked for the quotes also. (13 Replies)
Discussion started by: cokedude
13 Replies

8. UNIX for Dummies Questions & Answers

search for string and return substring

Hi, I have a file with the following contents: I need to create a script or search command that will search for this string 'ENDC' in the file. This string is unique and only occurs in one record. Once it finds the string, I would like it to return positions 101-109 ( this is the date of... (0 Replies)
Discussion started by: Lenora2009
0 Replies

9. Shell Programming and Scripting

Search for exact string

Hi All, I need to search in a csv file as mentioend in the Appendix A for a exact word lets "TEST". But using teh below command iam getting TEST1234, TEST12 and otehr entries as well. the problem is i check this condition to check to add a record to a table by making sure it does not... (16 Replies)
Discussion started by: rahman_riyaz
16 Replies

10. Shell Programming and Scripting

How do I search a File for a string exact match

Hi, Can you help please. I have the following comand: if ]; then l_valid_string="Y" fi The problem I am trying to solve is that my l_string = ABC and my file contains ABC ABC_EFG I only want back the value ABC exact match. (3 Replies)
Discussion started by: CAGIRL
3 Replies
Login or Register to Ask a Question