Find the answer to your Linux question:
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 ...
  1. #1
    Just 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
    Code:
    +--------------+-----+
    | phonenum     | grp |
    +--------------+-----+
    | +12125551234 |   1 | 
    | +19195551111 |   1 | 
    +--------------+-----+
    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?

    Thanks!

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

  3. #3
    Just Joined!
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by aazkan View Post
    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
    That sort of worked.
    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:~ $
    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:
    /usr/bin/gvoice -e $EMAIL -p $PASS send_sms  ${group1[0]} $MESSAGE
    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?

    Thanks again!

  4. #4
    Just 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

  5. #5
    Just Joined!
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by aazkan View Post
    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
    Great! This works swimmingly! Now, how can I set each value as a variable? Like g1p1, g1p2, g1p3, etc. Representing Group1 Phone 1.

  6. #6
    Just Joined!
    Join Date
    May 2011
    Location
    Malaysia
    Posts
    39
    Quote Originally Posted by RossIV View Post
    Great! This works swimmingly! Now, how can I set each value as a variable? Like g1p1, g1p2, g1p3, etc. Representing Group1 Phone 1.
    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.

  7. #7
    Just 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:
    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
    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?

    Thanks!

  8. #8
    Just Joined!
    Join Date
    May 2011
    Location
    Malaysia
    Posts
    39
    Quote Originally Posted by RossIV View Post
    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:
    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
    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?

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

  9. #9
    Just Joined!
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by aazkan View Post
    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.
    Great! That works perfectly!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...