regex in variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers regex in variable
# 1  
Old 08-08-2007
regex in variable

Hello!
I have problems with syntax...
I want a variable, that contains regex... is it possible?
Like $ a='.+->.+' and then use it in index ($0, "a").
I guess it won't work...

thank you Smilie
# 2  
Old 08-08-2007
Yes - use eval. For example, here I define a regex then use it with egrep
Code:
$ regex='.+->.+'
$ echo -e "somestring->someotherstring\ndont_match_this"
somestring->someotherstring
dont_match_this
$ echo -e "somestring->someotherstring\ndont_match_this" | eval egrep "\$regex"
somestring->someotherstring

Cheers
ZB
# 3  
Old 08-08-2007
Thank you!
I know, that must be simple, but I can't figure out , why it is not working...
I need a regex, that finds strings like "Aaaa->Bbbbb" but not "Aaaa-> Bbbbb"

I tried .+-+>+.+ but it finds also with spaces...
and .+-+>+[A-Z]+ finds totally wrong...
# 4  
Old 08-08-2007
Code:
$ echo 'Aaaa->Bbbbb' | egrep '.+-+>+[^ ]+'
Aaaa->Bbbbb
$ echo 'Aaaa-> Bbbbb' | egrep '.+-+>+[^ ]+'
$

# 5  
Old 08-08-2007
Thanks Smilie Now I got it Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Using RegEx with variable within bash if [[ ]]

I stumbled upon a problem, which I simplified to this: There is a list of numbers, stored in variable $LIST, lets use `seq 5 25` for demonstration. There is a number that should be compared against this list. For demonstration I use user input - read VALUE I am trying to compare RegEx... (2 Replies)
Discussion started by: Zorbeg
2 Replies

3. Shell Programming and Scripting

Nawk match regex of bash variable

Using a bash for loop to pass variables into a nawk loop to capture a string in an sftp log. Tried several different syntax methods to have the variable treated as a regex so the loop will capture the string. for i in `cat /tmp/dar3.out.2` do nawk -vst=$i '$5 ~ /$st/ && /closed/ && /user/... (3 Replies)
Discussion started by: smenago
3 Replies

4. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

5. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

6. Shell Programming and Scripting

awk: compose regex in variable and then use its contents like $0 ~ var

A question to the awk pundits: I was thinking about composing a regex in a variable and then use its contents like $0 ~ var instead of $0 ~ /r/. Sort of indirection. Did someone run into this? Is it possible at all? (3 Replies)
Discussion started by: RudiC
3 Replies

7. Shell Programming and Scripting

Grep: Searching with a regex that contains a variable from an array

I'm attempting to grep for lines formatted like this: grep -e '^\\",' Any suggestions as to why this isn't working? ---------- Post updated at 05:03 PM ---------- Previous update was at 04:17 PM ---------- This was my solution: grep -e '^\'\",' It's hard to read, but basically I... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

8. UNIX for Dummies Questions & Answers

Regex and variable assignment

Hi there, I really didn't want to have to waste people's time and ask this, but after 2 hours and running out of my own time I've decided to ask.. Basically I have a file in the format: word: other words; more words here; last word word: more words here; last word (etc) x 100 Where the... (3 Replies)
Discussion started by: maximus73
3 Replies

9. Shell Programming and Scripting

passing a regex as variable to awk and using that as regular expression for search

Hi All, I have a sftp session log where I am transferring multi files by issuing "mput abc*.dat". The contents of the logfile is below - ################################################# Connecting to 10.75.112.194... Changing to: /home/dasd9x/testing1 sftp> mput abc*.dat Uploading... (7 Replies)
Discussion started by: k_bijitesh
7 Replies

10. UNIX for Dummies Questions & Answers

regex on first string in a variable.

Hi all, this is driving me nuts. I need to evaluate if a variable in a shell script has a heading s or m character e.g. s92342394 or m9233489 if so then I need to get rid of them. I'm quite familiar with PERL and could do it there in 3 mins but I have not found a decent way to do this in a shell.... (1 Reply)
Discussion started by: Endo
1 Replies
Login or Register to Ask a Question