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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

regular expression help needed

  • 12-12-2005 2: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,571 ✭✭✭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