Regular Expressions in If clause


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regular Expressions in If clause
# 1  
Old 12-30-2008
Regular Expressions in If clause

Hi All,

I have a set of files in my directory like BED123C.txt, SED134F.txt,DEF567DF.txt. I want to execute separate scripts based on the file names.
I am using the following block of the code for this

for EachFile in `ls`
do
if [ EachFile = "B*" ]
then
sh Exe1.sh
fi
if [ EachFile = "S*" ]
then
sh Exe2.sh
fi
done

But it is not producing the desired result.
Can any one help me how to use regular expressions in if Condition in unix scripting?

Thanks in advance,

-- Raam.
# 2  
Old 12-30-2008
But why do you want to use if? The case statement is very powerful and is made to order for just such a situation.
Code:
case $(ls) in
    B*) sh Exe1.sh  ;;
    S*) sh Exe2.sh  ;;
esac

# 3  
Old 12-30-2008
You are missing a dollar sign in the if-statement, but I don't think it will work even then.

If you want to use if, you could experiment with the expr command or use something like this:
Code:
if [ ${EachFile:0:1} = B ]

# 4  
Old 12-30-2008
If for some reason you do not wish to use a case statement, here is one way of doing it using if statements. This works for bash and ksh93.
Code:
for eachFile in `ls`
do
   if [[ $eachFile =~ ^B ]]
   then
      ....
   fi
   if [[ $eachFile =~ ^S ]]
   then
      ....
   fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

2. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

3. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

4. Shell Programming and Scripting

Regular expressions help

need a regex that matches when a number has a zero (0) at the end of it so like 10 20 120 30 330 1000 and so on (6 Replies)
Discussion started by: linuxkid
6 Replies

5. Shell Programming and Scripting

Regular Expressions

what elements does " /^/ " match? I did the test which indicates that it matches single lowercase character like 'a','b' etc. and '1','2' etc. But I really confused with that. Because, "/^abc/" matches strings like "abcedf" or "abcddddee". So, what does caret ^ really mean? Any response... (2 Replies)
Discussion started by: DavidHe
2 Replies

6. Shell Programming and Scripting

Need help with Regular Expressions

Hi, In ksh, I am trying to compare folder names having -141- in it's name. e.g.: 4567-141-8098 should match this expression '*-141-*' but, -141-2354 should fail when compared with '*-141-*' simlarly, abc should fail when compared with '*-141-*' I tried multiple things but nevertheless,... (5 Replies)
Discussion started by: jidsh
5 Replies

7. UNIX for Advanced & Expert Users

Regular Expressions

Hi, below is a piece of code written by my predecessor at work. I'm kind of a newbie and am trying to figure out all the regular expressions in this piece of code. It is really a tough time for me to figure out all the regular expressions. Please shed some light on the regular expressions... (3 Replies)
Discussion started by: ramky79
3 Replies

8. UNIX for Dummies Questions & Answers

regular expressions

how to find for a file whose name has all characters in uppercase after 'project'? I tried this: find . -name 'project**.pdf' ./projectABC.pdf ./projectABC123.pdf I want only ./projectABC.pdf What is the regular expression that correponds to "all characters are capital"? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies

9. Shell Programming and Scripting

regular expressions

Hi, can anyone advise me how to shorten this: if || ; then I tried but it dosent seem to work, whats the correct way. Cheers (4 Replies)
Discussion started by: jack1981
4 Replies

10. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies
Login or Register to Ask a Question