passing regular expressions to mysql via $1


 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications passing regular expressions to mysql via $1
# 1  
Old 09-10-2008
passing regular expressions to mysql via $1

hi, unix and mysql gurus.

i'm trying to create a simple ksh script that accesses the mysql system database table, user. the idea is that the user can call the script with a regular expression to see some users, or call it without any options to see all users. this is what i have so far:


*****************************************************
mysql -BCNnqs --disable-pager -u system -pmanager << EOJ
use mysql;

set @regex = '$1';
select user from user where user REGEXP @regex;

quit
EOJ
*****************************************************


the problem, of course, is that if $1 is null, i get this error:

==> ERROR 1139 (42000) at line 8: Got error 'empty (sub)expression' from regexp

i changed my set statement to this, but it didn't work:


*****************************************************
set @regex = if(@regex is null, '^[a-z]','$1');
*****************************************************


does anyone know what i'm doing wrong? i'd appreciate your input. thanks.
# 2  
Old 09-10-2008
Quote:
Originally Posted by ankimo
Code:
mysql -BCNnqs --disable-pager -u system -pmanager << EOJ
use mysql;
 
set @regex = '$1';
select user from user where user REGEXP @regex;
 
quit
EOJ

I don't have access to this at the moment to test it, but you can put the entire select statement inside a variable:
Code:
QUERY="select user from user"
if [ -n "$1" ]; then
  QUERY="$QUERY where user REGEXP '$1'"
fi

mysql -BCNnqs --disable-pager -u system -pmanager << EOJ
use mysql;
$QUERY ;
quit
EOJ

And actually, you can specify the database on the command line, so that you pipe the command into mysql:
Code:
echo $QUERY | 
mysql -BCNnqs --disable-pager -d mysql -u system -pmanager

(I think it's the -d option. It might be -D).
# 3  
Old 09-10-2008
thank you, otheus! i really appreciate your help.
# 4  
Old 09-10-2008
Or you can pass in ${1-.} which evaluates to $1 unless it's empty, in which case the value is ".". (I imagine that's a better "catch-all" regex than the one you were using.)
# 5  
Old 09-10-2008
fantastic. thanks, era.
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

I need to pick a part of string lets stay started with specific character and end with specific character to replace using sed command the line is like this:my audio book 71-skhdfon1dufgjhgf8.wav' I want to move the characters beginning with - end before. I have different files with random... (2 Replies)
Discussion started by: XP_2600
2 Replies

2. 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

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

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

6. 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

7. 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

8. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 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. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies
Login or Register to Ask a Question