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

Unix Newbie Question

  • 22-07-2008 8:58am
    #1
    Registered Users, Registered Users 2 Posts: 3,875 ✭✭✭


    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 Posts: 3,875 ✭✭✭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 Posts: 3,875 ✭✭✭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