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

shell (ksh) scripting question

  • 15-06-2005 10: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,571 ✭✭✭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