Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

regular expression help needed

  • 12-12-2005 03:06PM
    #1
    Registered Users, Registered Users 2 Posts: 2,298 ✭✭✭



    hi

    i am using ksh in unix. i have set an environmental variable, X, to the following:

    [INDENT]
    # echo $X
    java full version "1.5.0_06-b03"
    [/INDENT]
    

    i am trying to do a simple check (using regular expression) in awk to see if X contains "1.5.0". i just need to know yes or no i.e. 0 for yes or >0 for no. i am doing the following:
    [INDENT]
    # echo $X | awk '$4 ~ /1.5.0/'
    java full version "1.5.0_06-b03"
    # 
    [/INDENT]
    
    as you can see, the awk prints the entire sting out if it does contain 1.5.0. if you check for 1.4.2 it does not print anything. in all cases, exit code is 0.

    is the another way i can use a regular expression i.e. awk, nawk or sed and just check the exit code for a yes or no answer? not doing this in perl (would prefer to but am not).

    --laoisfan


Comments

  • Registered Users, Registered Users 2 Posts: 2,298 ✭✭✭laoisfan



    just another thought, if not possible to check for 0 or >0, would it be possible to print out the 1.5.0 if X does contain 1.5.0 i.e print 1.5.0 not 1.5.0_06-b03.

    any takers?



  • Registered Users, Registered Users 2 Posts: 2,298 ✭✭✭laoisfan



    am looking at the index function with awk.....


  • Closed Accounts Posts: 25,848 ✭✭✭✭Zombrex


    its possible afaik to check if the value returned was true of false

    this might help
    http://www.unix.org.ua/orelly/unix/ksh/ch06_02.htm


  • Registered Users, Registered Users 2 Posts: 2,298 ✭✭✭laoisfan




    i have checked the return value and it is always 0 unless i am doing something wrong.

    at the moment i am looking at using index with awk. it returns 0 if no index is found else it returns a positiva number i.e. the value at which the index of the string your looking for starts in the main string.

    running into a wee problem with awk at the moment, hopefully that will be sorted soon....


  • Registered Users, Registered Users 2 Posts: 2,298 ✭✭✭laoisfan



    Using the following which seems to do what I need

    [INDENT]
    awk 'BEGIN {print index("abc 1.5.0-06-b05","1.5.0") }'
    3 <---- if i keep hitting return it keeps printing 3...never
    returns to command prompt unless i control-c!!
    [/INDENT]
    

    anyone?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,679 ✭✭✭daymobrew


    This spat out the right answer for me
    #!/bin/bash
    
    export X="java full version \"1.5.0_06-b03\""
    echo "\$X = $X"
    
    # Test if anything returned by grep.
    if [ -n "`echo $X | awk '{print $4}' | grep \"1\.5\.0\"`" ]
    then
      echo "\$X has 1.5.0."
    else
      echo "No 1.5.0 found."
    fi
    


Advertisement