Check whether the pattern is present or not?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check whether the pattern is present or not?
# 1  
Old 06-27-2007
Check whether the pattern is present or not?

I want to determine whether a specific pattern is present within a line or not

e.g.

The whole line is in a varaible called VALUE

VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"

i want to set a flag to 1 if i find the presence of ABC in the above variable.

Please tell me how to do it in a Shell Script?

Thanks in advance
# 2  
Old 06-27-2007
Code:
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c 'ABC'`
echo "mFlag = "$mFlag

# 3  
Old 06-27-2007
Quote:
Originally Posted by skyineyes
I want to determine whether a specific pattern is present within a line or not

e.g.

The whole line is in a varaible called VALUE

VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"

i want to set a flag to 1 if i find the presence of ABC in the above variable.

Please tell me how to do it in a Shell Script?

Thanks in advance
Code:
if [[ "$VALUE" = *ABC* ]] ; then
  flag=1
fi;

# 4  
Old 06-27-2007
Thanks alot

Just a modification to this.

If i want to test the presence of two patterns in one go then how this line should look like. Suppose i want to check the presence of both "ABC" and "PAR" and then set the value if EITHER ONE OF THEM OR BOTH EXIST

i.e LOGICAL OR CONDITION FOR PRESENCE OF BOTH.
# 5  
Old 06-27-2007
Code:
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c 'ABC|PAR'`
echo "mFlag = "$mFlag

# 6  
Old 06-27-2007
Thanks alot man.
I am really thankful

Quote:
Originally Posted by Shell_Life
Code:
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c 'ABC|PAR'`
echo "mFlag = "$mFlag

# 7  
Old 06-27-2007
If you test the presence of 'GQS' the egrep command will set the flag to 1, because the 'GQS' is found (inside 'AGQSAs').
If you want to test for the entire value, you must modify the egrep RE:

Code:
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c '[(, ](XXX|GQS)([),]|$)'`
echo "mFlag = "$mFlag

The result will be 0 since there is no element equal to GQS nor XXX inside VALUE
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. Shell Programming and Scripting

How to remove content present in between specific pattern ?

Hi, I have a file with following pattern. We are looking to filter out only specific content from this file. sample BLAdmins Server.* LinuxAdmins Server.* Policy Name: Recommended Default ACL Policy Everyone ACLPushJob.Read Everyone ACLTemplate.Read Everyone ... (9 Replies)
Discussion started by: Litu19
9 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

Check if file is present using input from another file

Hello, I have a comma delimited file such as: cat /statistics/support/input.txt ID,Serial,quantitity,atribute1,atribute2 1,89569698,5,800,9900, 1,35568658,8,1200,5550 1,89569698,8,320,5500 1,68753584,85,450,200 ID should always have 1 digit, Serial 8 digits, and the others may... (2 Replies)
Discussion started by: alex2005
2 Replies

5. Shell Programming and Scripting

search pattern present in second field

Hi All, I have a file with following list. example 1 ======== cat 1.txt -------- 0000cab4752c 0000dab47c2c ... ... ... Also i have another file 2.txt in which the data is in this format as shown: cat 2.txt ---------... (6 Replies)
Discussion started by: imas
6 Replies

6. Shell Programming and Scripting

How to Check whether list file present in TXT file exist or not

Hi All, I have txt file which has list of files. I have to check whether these files exist or not. Thanks supriya (6 Replies)
Discussion started by: supriyabv
6 Replies

7. Shell Programming and Scripting

check whether 3 files are present

I'm trying to check whether 3 files are existing and send 3 files as attachements. If only two are there then send those two files as attachments. if ; then elif ; then I tired the above given syntax and then it is giving me an error line 11: ' I tried with -a instead of && and... (3 Replies)
Discussion started by: Celvin VK
3 Replies

8. Shell Programming and Scripting

How to check the variable is present in array or not ?

Hi , I am trying to check wether the variable is present in the array. please see the below code .when ever i do this its taking only the first value of the array . please advise. ###Code Snnipet ### #!/bin/ksh set -xv if ]; then echo " you have Specified the ORG ID - $1 " ... (1 Reply)
Discussion started by: padhu.47
1 Replies

9. UNIX for Dummies Questions & Answers

how to check if path is present?

following situation... - bourne shell script - sb. should entry a path and the script should look if the path exists, when not it should restart the Input ... echo "path\c" read Inp if ; then echo " path doesn't exist, try again... " (how to go back to the Inp?????) else echo "... (3 Replies)
Discussion started by: svennie
3 Replies
Login or Register to Ask a Question