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.

Unix Newbie Question

  • 22-07-2008 09:58AM
    #1
    Registered Users, Registered Users 2, Paid Member Posts: 3,981 ✭✭✭


    Hi All

    I have scripting question.
    I have a file "out.txt"

    which is generated by another script

    the file contains the following
    my_identifier8859574
    
    logout
    

    The number is generated in the script and I have put the my_identifier bit in front of it as a unique identifier

    I now have another script which uses this file, It needs the number Only that comes after my_identifier

    I havn't a breeze how to do this.
    I assume i need to use awk

    something like

    VALUE=cat out.txt | grep my_identifier | awk .....

    any suggestions?
    Cheers
    Graham


Comments

  • Registered Users, Registered Users 2 Posts: 368 ✭✭backboiler


    If the text is actually "my_identifier" (13 characters long) then you can use
    head -1 out.txt | cut -c14-
    
    That gives the 14th and subsequent characters of the first line of the out.txt file. If the text is variable length then add a space or other divider between the fields e.g. "my_identifier:733739" or "37efh8:73654343" use ":" as the field separator, then you can use
    head -1 out.txt | cut -f2 -d':'
    
    That takes the second field (-f instead of -c) and says the delimiter is a colon. You can use any character.

    If you want to put the result in a shell variable then enclose the whole thing in "$(...)", i.e.
    variable=$(head -1 out.txt | cut -f2 -d':')
    


  • Registered Users, Registered Users 2, Paid Member Posts: 3,981 ✭✭✭Beta Ray Bill


    Im not sure this will 100% work

    As if the script that generates the out.txt throws up and oracle error its is very likey that a ':' will occur in the first line and hence the script which uses out.txt will be processing incorrect data.

    that is why I user my_identifier as a unique identifier that could not occur if the script which generates out.txt throws up an error

    for example if an ORA-0600:some_problem occured the second script would pull back the wrong data

    Graham


  • Registered Users, Registered Users 2 Posts: 368 ✭✭backboiler


    I'm not really familiar with awk's syntax but perl is supposedly similar. In perl it'd be
    perl -ne 'print $1 if /my_identifier(\d+)/' out.txt
    
    (assuming that the bit you're looking for is numeric, "\d+" can change to ".+" for all characters to the end of the line or "\S" (capital 'S') for non-whitespace characters).


  • Registered Users, Registered Users 2, Paid Member Posts: 3,981 ✭✭✭Beta Ray Bill


    figured this one out

    its
    VALUE="$(fgrep my_identifier out.txt)" VALUE="${VALUE#my_identifier}"
    


  • Registered Users, Registered Users 2 Posts: 368 ✭✭backboiler


    Very tidy altogether!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,065 ✭✭✭Snowbat


    grep 'my_identifier' out.txt | sed 's/my_identifier//'


Advertisement