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

shell (ksh) scripting question

Options
  • 15-06-2005 11:37am
    #1
    Registered Users Posts: 2,297 ✭✭✭



    Hi

    Am having a problem with what seems like a trivial operation!! I know doing it in perl would be a lot easier but unfortunately this has to be done in shell (ksh)!!

    Anyway, back to the problem.

    I want to be able to check the first character of a word e.g

    pXXXX or iXXXX
    ^ ^

    How do I check that the word starts with a p or i ? This is wrecking my head.
    Doing a google but no look so far....

    Much appreciated, laoisfan



Comments

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




    Hi am trying the following but not sure if it is a valid method...

    $ export X=p1234
    $ echo $X
    p1234
    $ echo $X | awk /^p/
    p1234

    awk prints out $X if it contains 'p' at the start

    If I search for say 'i'...

    $ echo $X
    p1234
    $ echo $X | awk /^i/
    $

    awk does not print anything as $X does not start with 'i'

    What are people's opinions on this method?

    --laoisfan



  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    I got this working on Solaris 9.
    #/bin/ksh
    
    X=p1234
    
    if [ -n "`echo $X | grep ^[pi]`" ]
    then
      echo "FOUND"
    else
      echo "Not found"
    fi
    
    This reports "FOUND". Works with /bin/bash too. Got an error with /bin/sh but didn't bother to find out why.

    I chose grep over awk because it is a smaller binary (10k vs 83k) and therefore should load and get running faster.


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



    Thanks
    I will use that, wanted to use grep anyway!!

    Much appreciated, laoisfan


Advertisement