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.

shell (ksh) scripting question

  • 15-06-2005 11:37AM
    #1
    Registered Users, Registered Users 2 Posts: 2,298 ✭✭✭



    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, Registered Users 2 Posts: 2,298 ✭✭✭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, Registered Users 2 Posts: 6,652 ✭✭✭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, Registered Users 2 Posts: 2,298 ✭✭✭laoisfan



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

    Much appreciated, laoisfan


Advertisement