Comparison between variable and regexp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparison between variable and regexp
# 1  
Old 03-24-2010
Lightbulb Comparison between variable and regexp

Hi all,
How I could compare variable and regexp?
For example...
I am running the script ./aaa.sh -b [0-9]*
[0-9]* is variable $2 (second argument of script).

I would compare variables $a (generated from my cycle) and $2 as regexp.
I need from regular expression find file that satisfies this regexp.
I tried classic if [ "$a"="$2" ];... or if [ expr $a:$2 ]; ....but it isn't running ("Too many arguments")

Any ideas?

Last edited by Koblenc; 03-24-2010 at 04:46 PM..
# 2  
Old 03-24-2010
Try:
Code:
a=10
case $a in $2) echo variable \$a matches "$2"; esac

Use quoting around the pattern to pass it to the script:
Code:
$ ./aaa.sh -b "[0-9]*"
variable $a matches [0-9]*

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk if comparison with variable

Hi , Need help, p for value in `awk -F, '{print $1 }' ad | uniq` do x=$(echo $value) echo $x echo `awk -F, '{if( $1 == $x) sum = sum + $8 } END{print sum}' ad` --- not working echo `awk -F, '{if($1 == “MT”) sum = sum + $8 } END{print sum}' ad` -- Working but hard coded done; ad... (4 Replies)
Discussion started by: nadeemrafikhan
4 Replies

2. Shell Programming and Scripting

Regexp

I would like to extract "1333 Fairlane" given the below text. The word "Building:" is always present. The wording between Building and the beginning of the address can be almost anything. It appears the the hyphen is there most of the time. Campus: Fairlane Business Park Building:... (9 Replies)
Discussion started by: bbaker@copesan.
9 Replies

3. UNIX for Advanced & Expert Users

awk variable regexp works in AIX but not in SunOS?

Using awk variables for regular expressions is working for me in AIX. It is failing for me in SunOS. I don't know why. Can someone explain and/or suggest a fix for the SunOS version? Here is a little test script. It runs fine in AIX: $ cat test.ksh #! /bin/ksh print "Executed on OS: $(... (6 Replies)
Discussion started by: charles_n_may
6 Replies

4. Shell Programming and Scripting

Variable comparison in 'if' with a string.

Hi All, I want to compare the variable value with string in a shell script. my code is: for var in $packsName do if then echo $var fi done But I am getting error as: ABC : not found. Am I going wrong somwhere? please guide. Thanks. (5 Replies)
Discussion started by: AB10
5 Replies

5. Shell Programming and Scripting

Strange variable comparison result in awk

So, I'm making a little awk script that generates a range-based histogram of a set of numbers. I've stumbled onto a strange thing. Toward the end of the process, I have this test: if ( bindex < s ) "bindex" is the "index" of my "bin" (the array element that gets incremented whenever a... (2 Replies)
Discussion started by: treesloth
2 Replies

6. Shell Programming and Scripting

Awk multiple variable array: comparison

Foo.txt 20 40 57 50 22 51 66 26 17 15 63 18 80 46 78 99 87 2 14 14 51 47 49 100 58 Bar.txt 20 22 51 15 63 78 99 55 51 58 How to get output using awk 20 22 57 50 51 15 26 17 63 78 80 46 99 55 - - 51 58 49 100 (5 Replies)
Discussion started by: genehunter
5 Replies

7. Shell Programming and Scripting

Variable comparison

Can anyone help me with this section of code? The scenario is a value drops from A to B or A to C or B to C. If it drops from A to B or B to C, we print "Drop one level" If it drops from A to C, we print "Dropped two levels". The problem is script is throwing error when comparing variable... (2 Replies)
Discussion started by: sundar63
2 Replies

8. Shell Programming and Scripting

awk comparison with variable

hi, I want to compare i variable in the awk statement but its not working out. Pl help me out If we do the comparison like this its OK, cat sample | awk -F" ", '{if ($1=="1-Sep-2009") print $1,$2,$3,$4,$5}' But if u use a variable instead of "1-Sept-2009", it does not return anything,... (2 Replies)
Discussion started by: asadlone
2 Replies

9. Shell Programming and Scripting

variable comparison

Hello, I am trying to compare two variables, which both have a word stored in it. The code I am using is. Please tell me which one of these is correct.Or please tell me the correct syntax. Thankyou if then if then if then if then if then None of them seems to work... (3 Replies)
Discussion started by: Freakhan
3 Replies

10. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies
Login or Register to Ask a Question