Compare pattern in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare pattern in variable
# 1  
Old 07-31-2013
Compare pattern in variable

How to check if pattern is matching in variable or not.
I want to check file name in variable with the pattern.

Eg: file_name="AB1000.csv"

Check for below patterns
Code:
pattern1 = AB???
pattern2 = abc??*

if  file_name == <patten1>
then
       ...
elif  file_name == <pattern2>
     ---
fi

Please advise. Thanks.
Moderator's Comments:
Mod Comment You have repeatedly been asked to use CODE tags when posting code, sample input data, and sample output data. Please pay attention to this simple request for you to make it easier for us to help you.

Last edited by Don Cragun; 07-31-2013 at 03:04 PM.. Reason: CODE Tags
# 2  
Old 07-31-2013
What system/OS/shell do you use? bash, for instance, allows for matching a variable to a regular expression.
# 3  
Old 08-01-2013
I am using AIX... korn shell. Please advise.
# 4  
Old 08-01-2013
Code:
DIR=/your/directory/to/search
pattern1="AB???"
ls $DIR/$pattern1 2>/dev/null || echo "No file found matching the pattern"

To perform action based on the pattern match use the below.
Code:
DIR=/your/directory/to/search
pattern1="AB???"
ls $DIR/$pattern1 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
  echo "Match found"
else
  echo "No match found"
fi

# 5  
Old 08-02-2013
I think you are searching within the variable, rather that for an actual file, so:-

You could try a case statement if you are planning lots of if-then-elif then-elif.....
Code:
file_name="AB1000.csv"
case file_name in
   AB???*)  Do the first AB action
          Do the second AB action ;;
   abc??*)  Do the first abc action
          Do the second abc action ;;
   *)    Do actions for no match ;;
esac

Of you you prefer, you can use the variable substitution commands to test if your string contains something interesting:-
Code:
file_name="AB1000.csv"

test_file_name="${file_name#AB}"

if [ "$test_file_name" != "$file_name" ]
then
   echo "Matched AB"
fi


test_file_name="${file_name#abc}"

if [ "$test_file_name" != "$file_name" ]
then
   echo "Matched abc"
fi


Does this help, or have I missed the point?


Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

2. UNIX for Beginners Questions & Answers

Compare two files when pattern matched

I have two files say FILE1 and FILE2. FILE1 contains 80,000 filename in sorted order and another file FILE2 contains 6,000 filenames is also in sorted order. I want to compare the filename for each file and copy them in to a folder when filename is matched. File1.txt contain 80,000... (8 Replies)
Discussion started by: imranrasheedamu
8 Replies

3. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

4. Shell Programming and Scripting

Script to compare pattern and print a different pattern in each line

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ......qrt: .... .......xyz: abz: ......qrt: ... I have tried using awk and cut, but the position of these values keep changing, so I wasn't able to get... (2 Replies)
Discussion started by: Serena
2 Replies

5. Shell Programming and Scripting

Compare values for a pattern match and execute script

Here in the input file 23:59:13,devicename,21,server1,700 23:59:13,devicename,22,server2,200 23:59:13,devicename,23,server3,200 23:59:13,devicename,24,server4,200 23:59:13,devicename,25,server5,200 23:59:13,devicename,26,server6,200 23:59:13,devicename,27,server7,200... (6 Replies)
Discussion started by: necro98
6 Replies

6. Shell Programming and Scripting

Compare the two variable with if condition

Please help me with this: I need to compare two values in if condition in shell script but its goes always to else condition: TIME_CHECK=PM TIME-CLOCK=PM if ; then echo "You have access!" else echo "ACCESS DENIED!" fi (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

7. Red Hat

Compare Empty Variable

Hello All, I am running the below code in my script. I want if jk is empty nothing should be appened to the file total_usage. but apparently its not happening.Kindy let me know how to do it. ################################### jk=`ps auxf |grep -w $inputline|tr -s " "|cut -d... (0 Replies)
Discussion started by: ajaincv
0 Replies

8. Shell Programming and Scripting

Shell Scripting: Compare pattern in two files and merge the o/p in one.

one.txt ONS.1287677000.820.log 20Oct2010 ONS.1287677000.123.log 21Oct2010 ONS.1287677000.456.log 22Oct2010 two.txt ONS.1287677000.820.log:V AC CC EN ONS.1287677000.123.log:V AC CC EN ONS.1287677000.820.log:V AC CC EN In file two.txt i have to look for pattern which column one... (17 Replies)
Discussion started by: saluja.deepak
17 Replies

9. Shell Programming and Scripting

compare two variable value in unix

dear all i had two variable in my script. var1 and var2 var1= 10 ( it will part of date op ) Fri Aug 8 10:05:09 IST 2008 var2=9 ( it will op of ls -ltr and time part of it.) now i want to compare of them and want to do some task if both are are not same. kindly let me know possible... (0 Replies)
Discussion started by: jaydeep_sadaria
0 Replies

10. Shell Programming and Scripting

Compare string to a pattern

I am new to unix and need to learn how to compare a variable $subject to a string pattern. If the variable has the word "Item" in it then it should be true. How do I do this? Currently I am using the Bourne shell but I can also use Korn or Bash. I come from a Rexx background where strings are... (2 Replies)
Discussion started by: jerryte
2 Replies
Login or Register to Ask a Question