Results 1 to 8 of 8

Thread: grep only filename from log file?

  1. #1
    Join Date
    Jun 2006
    Location
    Sweden
    Beans
    69
    Distro
    Ubuntu 10.10 Maverick Meerkat

    grep only filename from log file?

    I need some help. I'm trying to grep only the filename (incl. path) from a log.

    ORA-00060: Deadlock detected. More info in file /oracle/admin/ARNDB/udump/arndb_ora_11957.trc.

    or the line:
    ORA-00060: Deadlock detected. More info in file /oracle/admin/ARNDB/udump/arndb_ora_1157.trc.


    And the result should be:
    /oracle/admin/ARNDB/udump/arndb_ora_11957.trc

    or

    /oracle/admin/ARNDB/udump/arndb_ora_1157.trc
    ?
    "I have come to believe that the whole world is an enigma,
    a harmless enigma that is made terrible by our own mad attempt
    to interpret it as though it had an underlying truth."

  2. #2
    Join Date
    Oct 2008
    Location
    Stuttgart, Germany
    Beans
    441
    Distro
    Ubuntu Development Release

    Re: grep only filename from log file?

    if the input is consistent, try:

    cat <log file> | grep ORA-00060 | awk '{print $8}'

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: grep only filename from log file?

    You can use grep's -o option to print only the matched parts, something like:
    Code:
    grep -o -e '/.*[^.]' filename
    You might need to use another grep to filter out some lines:
    Code:
    grep -e '^ORA-00060:' file | grep -o -e '/.*[^.]'
    In awk you can try something like:
    Code:
    awk '/^ORA-00060:/{print $NF}' filename
    or to get rid of the extra period from the end of the filename:
    Code:
    awk '/^ORA-00060:/{print substr($NF, 0, length($NF)-1)}' filename

  4. #4
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: grep only filename from log file?

    Quote Originally Posted by Wayne_V View Post
    if the input is consistent, try:

    cat <log file> | grep ORA-00060 | awk '{print $8}'
    tisk tisk, no need to cat file | grep pattern

    Just grep

    grep pattern file

    Worse , no need to grep with awk

    I was going to suggest awk is a better tool for this, but now I see sisco311 has stole my thunder.
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  5. #5
    Join Date
    Jun 2006
    Location
    Sweden
    Beans
    69
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: grep only filename from log file?

    Thank you!

    My current command is like this:

    Code:
    tail -n 500 /oracle/admin/ARNDB/bdump/alert_ARNDB.log |awk '/^ORA-00060:/{print substr($NF, 0, length($NF)-1)}'
    Suggested grep command also works fine.

    Now I just need to cat the returned filename. If I use:

    Code:
    cat `tail -n 500 /oracle/admin/ARNDB/bdump/alert_ARNDB.log |awk '/^ORA-00060:/{print substr($NF, 0, length($NF)-1)}'`
    It works fine if there is a ORA-00060 line in the alert log. But if not the command just hangs.

    How can this be solved?
    "I have come to believe that the whole world is an enigma,
    a harmless enigma that is made terrible by our own mad attempt
    to interpret it as though it had an underlying truth."

  6. #6
    Join Date
    Oct 2008
    Location
    Stuttgart, Germany
    Beans
    441
    Distro
    Ubuntu Development Release

    Re: grep only filename from log file?

    an easy way would be to just assign the file name to a variable and then check it exists before trying to cat it:

    FILE=`tail -n .....`

    if [ -f $FILE ]
    then
    cat $FILE
    else
    echo "no ORA-00060"
    fi

  7. #7
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: grep only filename from log file?

    Code:
    while read-r filename
    do
        cat "$file"
    done< <(tail -n 500 logfile | awk ...)

  8. #8
    Join Date
    Aug 2011
    Beans
    4

    Re: grep only filename from log file?

    Quote Originally Posted by Jungleboss View Post
    I need some help. I'm trying to grep only the filename (incl. path) from a log.

    ORA-00060: Deadlock detected. More info in file /oracle/admin/ARNDB/udump/arndb_ora_11957.trc.

    or the line:
    ORA-00060: Deadlock detected. More info in file /oracle/admin/ARNDB/udump/arndb_ora_1157.trc.


    And the result should be:
    /oracle/admin/ARNDB/udump/arndb_ora_11957.trc

    or

    /oracle/admin/ARNDB/udump/arndb_ora_1157.trc
    ?
    grep -o '/.*' /path/to/file
    should work or
    grep -o '/.*.trc' /path/to/file
    if you want only the paths leading to .trc files

    ---
    Cheers!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •