Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

regular expression help needed

Options
  • 12-12-2005 3:06pm
    #1
    Registered Users Posts: 2,297 ✭✭✭



    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 Posts: 2,297 ✭✭✭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 Posts: 2,297 ✭✭✭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 Posts: 2,297 ✭✭✭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 Posts: 2,297 ✭✭✭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 Posts: 6,508 ✭✭✭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