Search code examples
unixawksedgrepcut

Display matched string to end of line


How to find a particular string in a file and display matched string and rest of the line?

For example- I have a line in a.txt:

This code gives ORA-12345 in my code.

So, I am finding string 'ORA-'

Output Should be:

ORA-12345 in my code

Tried using grep:

grep 'ORA-*' a.txt

but it gives whole line in the output.


Solution

  • # Create test data:
    echo "junk ORA-12345 more stuff" > a.tst
    echo "junk ORB-12345 another stuff" >> a.tst
    # Actually command:
    # the -o (--only-matching) flag will print only the matched result, and not the full line
    cat a.tst | grep -o 'ORA-.*$' # ORA-12345 more stuff
    

    As fedorqui pointed out you can use:

    grep -o 'ORA-.*$' a.tst