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 all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
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

substring aix

  • 16-12-2009 6:01pm
    #1
    Registered Users, Registered Users 2 Posts: 169 ✭✭


    Hi

    How do i print only section of a string?

    Eg mystring="hellooo"

    How do i make $mystring print only this portion of the string:
    hello


    cheers
    db

    ksh aix 5


Comments

  • Registered Users, Registered Users 2 Posts: 2,747 ✭✭✭niallb


    Depends on how you pick the hello out of hellooo
    If you mean characters number 1 to 5, then 'cut' should exist in an AIX environment.

    Does 'cut -c 1-5 ' give you an error ?


  • Registered Users, Registered Users 2 Posts: 868 ✭✭✭brianmc


    Hi

    How do i print only section of a string?

    Eg mystring="hellooo"

    How do i make $mystring print only this portion of the string:
    hello


    cheers
    db

    ksh aix 5

    In ksh you can use modifiers in the variable substitution...

    one of the ways of modifying things is to use pattern matching to cut certain parts of the value off... So I don't know if this is going to help but I'll explain it anyway and you can decide.

    The following prints out the word "walk" because you're removing the "ing" part using pattern matching.
    X=walking
    
    echo ${X%%ing}
    

    The following prints "walking" because the pattern doesn't match at the end of the value...
    X=walking
    
    echo ${X%%foo}
    

    If you want to match at the start of a value (remove bits from the start of the value), you use # characters instead of % characters.

    The following prints "king"...
    X=walking
    
    echo ${X##wal}
    

    You can use pattern matching characters similar to the filename matching metacharacters so for example if you wanted to leave out the first 4 characters the following would print "ing"...
    X=walking
    
    echo ${X##????}
    

    I suspect all of this might be backwards to what you're requiring though.

    In that case you could probably use 'cut' like the previous post, or use 'sed'

    The following would print the first 4 characters (i.e. "walk")
    X=walking
    
    echo $X | sed 's/^\(....\).*/\1/'
    

    (All of the above is off the top of my head so I may have a "typo" in there somewhere) but I don't think so :)

    Edit: If you've a newer version of ksh than I suspect you have there is a substring type of substitution...

    Something like ${X:5} but I don't use it myself because in most commercial environments (such as AIX) you are likely using an older version of ksh.


  • Registered Users, Registered Users 2 Posts: 169 ✭✭DonnieBrasco


    thanks - will try each of the replies out tomorrow.

    actually reading through them all looks like some might work better for what im actually trying to achieve.

    thanks
    The following would print the first 4 characters (i.e. "walk")
    echo $X | sed 's/^\(....\).*/\1/'
    

    what else can banging on your keyboard do in unix ?!!:)


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    I like AWK:
    X="I love Unix, said the student"
    echo $X | awk '{ print substr($0,8,4) }'
    
    Where 0 represents the first input string field.
    8,4 represents the start and end range.

    "Unix" which is your sub string should be printed
    to stdout. AWK is a pretty underrated gem sadly:(


  • Registered Users, Registered Users 2 Posts: 1,606 ✭✭✭djmarkus


    Naikon wrote: »
    I like AWK:
    X="I love Unix, said the student"
    echo $X | awk '{ print substr($0,8,4) }'
    
    Where 0 represents the first input string field.
    8,4 represents the start and end range.

    "Unix" which is your sub string should be printed
    to stdout. AWK is a pretty underrated gem sadly:(
    Is that not because it likes to eat lots and lots of memory?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,721 ✭✭✭E39MSport


    Theres some good general stuff on here. Unix related but a good reference I think.

    http://www.livefirelabs.com/unix_tip_trick_shell_script/unix_tip_archive.htm


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    djmarkus wrote: »
    Is that not because it likes to eat lots and lots of memory?

    AWK does not use THAT much memory;)
    Not that you should write enterprise apps in it.

    Still, perl probably crunches more memory.
    AWK remains fairly simple and much easier
    to master for a newb in comparison to Perl.


  • Registered Users, Registered Users 2 Posts: 868 ✭✭✭brianmc


    Naikon wrote: »
    AWK does not use THAT much memory;)
    Not that you should write enterprise apps in it.

    Still, perl probably crunches more memory.
    AWK remains fairly simple and much easier
    to master for a newb in comparison to Perl.

    I think the point is that if you can do the same job with a couple of simple inbuilt shell commands, that launching awk is likely to generate a higher overall load. Second process, context switching, etc. That said, the shell does do some stuff quite a bit more inefficiently than the relevent dedicated tool and so the difference is probably negligible either way.

    Finally, I usually also tell people that while efficiency is good, if their shell script is getting to the point where they're having to focus on efficiency, they're probably using the wrong tool in the first place.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Naikon wrote: »
    AWK does not use THAT much memory;)
    Not that you should write enterprise apps in it.

    Still, perl probably crunches more memory.
    AWK remains fairly simple and much easier
    to master for a newb in comparison to Perl.

    Yeah, but the effort invested in perl reaps far far greater rewards in the medium term. That said, sed and awk will be the things I invest my next amount of unix learning effort in (before embarking on certification of some kind).

    I wouldn't be concerned with memory usage in a shell script. It's not really gonna be an issue, or you should be using a shell script. :)


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    brianmc wrote: »
    I think the point is that if you can do the same job with a couple of simple inbuilt shell commands, that launching awk is likely to generate a higher overall load. Second process, context switching, etc. That said, the shell does do some stuff quite a bit more inefficiently than the relevent dedicated tool and so the difference is probably negligible either way.

    Finally, I usually also tell people that while efficiency is good, if their shell script is getting to the point where they're having to focus on efficiency, they're probably using the wrong tool in the first place.

    True enough. Context switching is expensive.

    Portability would be the main concern for me where
    AWK is bound to be found on most POSIX systems unlike
    Perl which "may" be a third party install.

    "Learning Perl" is a pretty enjoyable read if I must say
    so myself. Great tool for alot of tasks as Khannie mentioned...like this


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    lol. Love that script. :D

    I wouldn't be concerned with the expense of context switching on modern machines either (and it shouldn't be an issue on most multicore boxes). Again if it's a bottleneck for you, you probably shouldn't be using a shell script. I think people spend too much time pre-optimising instead of doing it the way that's fastest for them. CPU cycles are cheap. Humans are expensive. :)

    The one thing I hate about perl is that most people write nasty perl code which makes use of obscure "features" in the language, rendering it impossible to read.


Advertisement