Results 1 to 9 of 9
Hello!
I am attempting to issue a MySQL command and parse/sanitize the output in a script on CentOS 5.5.
The MySQL command results in
Code:
+--------------+-----+
| phonenum | grp ...
- 05-09-2011 #1Just Joined!
- Join Date
- May 2011
- Posts
- 8
How to parse text?
Hello!
I am attempting to issue a MySQL command and parse/sanitize the output in a script on CentOS 5.5.
The MySQL command results in
What command would I need to issue to select only the phone number from each line beginning with the + and ending at the last digit of the phone number?Code:+--------------+-----+ | phonenum | grp | +--------------+-----+ | +12125551234 | 1 | | +19195551111 | 1 | +--------------+-----+
Thanks!
- 05-11-2011 #2Just Joined!
- Join Date
- May 2011
- Location
- Malaysia
- Posts
- 39
Hi,
Perhaps this might help.
(I'm assuming you're running something like this: mysql -A -u root -p -e "some mysql cmds;" somedatabasename)
Pipe your mysql command to awk like so;
| awk 'NR>=1&&NR<=4'
It should list the items you want
- 05-12-2011 #3Just Joined!
- Join Date
- May 2011
- Posts
- 8
That sort of worked.
All I want left is the phone numbers. Also, say there were 50+ phone numbers. Is there a command that I could issue that would return the number of phone numbers so that a command like the following wouldn't have to exist for each number?Code:root@piaf-purple:~ $ mysql -uroot -p -e"select phonenum,grp from gvsms.recipients where grp='1'" | awk 'NR>=1&&NR<=4' phonenum grp +12125551234 1 +19195551111 1 root@piaf-purple:~ $
Right now I have to have one line like that for each number in each group. MESSY! Is there a way that I can automatically generate the appropriate number of lines with the appropriate information?Code:/usr/bin/gvoice -e $EMAIL -p $PASS send_sms ${group1[0]} $MESSAGE
Thanks again!
- 05-12-2011 #4Just Joined!
- Join Date
- May 2011
- Location
- Malaysia
- Posts
- 39
Code:root@piaf-purple:~ $ mysql -uroot -p -e"select phonenum,grp from gvsms.recipients where grp='1'" | awk 'NR>=1&&NR<=4' phonenum grp +12125551234 1 +19195551111 1 root@piaf-purple:~ $
Hi RossIV
Could you try it with these instead and see if this is what you're looking for?
Code:mysql -u root -p -Be "select phonenum,grp from gvsms.recipients where grp='1'" | awk ' $NF {print $1}' |grep -v phonenum
- 05-13-2011 #5Just Joined!
- Join Date
- May 2011
- Posts
- 8
- 05-14-2011 #6Just Joined!
- Join Date
- May 2011
- Location
- Malaysia
- Posts
- 39
Hmm.. I reckon you can go with any of this options:
a. put into a bash array but perhaps you need to trick bash into multiarray
b. somehow pipe it into perl and use perl's array
But if there's any shell guru around I'm certain they would probably can suggest something easier.
Sorry mate, am about to start on dawn of war (after putting the kids to sleep) so I'll catch you later.
- 05-15-2011 #7Just Joined!
- Join Date
- May 2011
- Posts
- 8
I have temporarily found a resolution to the variable problem, and therein lies the problem.
The workaround is to write the phone numbers to a file and then have a command read them back to my application.
I can't seem to get the file written.
This is what I am using:
That just gives me a blank file. It seems that the echo command is not picking up the previous data. How can I get a file (over)written with the data that this command passes?Code:mysql -u root -p -Be "select phonenum,grp from gvsms.recipients where grp='1'" | awk ' $NF {print $1}' |grep -v phonenum |echo > /var/www/cgi-bin/gvsms
Thanks!
- 05-16-2011 #8Just Joined!
- Join Date
- May 2011
- Location
- Malaysia
- Posts
- 39
Try it this way instead
mysql -u root -p -Be "select phonenum,grp from gvsms.recipients where grp='1'" | awk ' $NF {print $1}' |grep -v phonenum >> /var/www/cgi-bin/gvsms
>> appends it in case there is already data in /var/www/cgi-bin/gvsms
If you rather use echo perhaps something like this
somedata=`mysql -u root -p -Be "select phonenum,grp from gvsms.recipients where grp='1'" | awk ' $NF {print $1}' |grep -v phonenum`
echo $somedata (so it shows to the screen)
or pipe it to your /var/www/cgi-bin/gvsms like so
echo $somedata >> /var/www/cgi-bin/gvsms
btw, i'm assuming you're using bash as your shell tho this should work with any other shell. I use zsh btw.
- 05-17-2011 #9


Reply With Quote
