UNIX Shell cut, grep, sed

Posted on 2011-09-30
Last Modified: 2012-05-12
I have a line on field 6 that contains two Oracle errors.  I'd like to only cut the words "ORA-01653" and "ORA-12096"  How do I achieve it? (the Oracle errors can be different each time the job runs)  The line is pipe delimited.

Shedule Job,Daily Schedule Job|Data Load-IL|28-SEP-10|28-SEP-10|Failed|ORA-12096: error in view log on T_RATE_PLAN" ORA-01653: unable to extend table MLOG$_T_RATE_PLAN by 1024 in tablespace SYSTEM|
Question by:scpig

Expert Comment

ID: 36894230
regular expression

    A way of specifying and matching strings for shells (filename wildcarding), grep (file searches), sed, and awk.
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 36894500
Hi scpig, by cutting I understand that you want to filter out the ORA messages. I'm assuming that your lines are in a file called test.txt

This is what you will do the trick::

cut -d"|" -f6 test.txt | grep -o "ORA-[0-9]*"

Open in new window

First part will cut out field #6 with | as a field separator in your file test.txt. Second part will grep for the ORA error numbers and print them each on a new line.

The command above on your line above will produce this output:

Open in new window


Author Comment

ID: 36894668
When I tried it, it gave me this error:
cut -d"|" -f6 test.txt | grep -o "ORA-[0-9]*"

grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .

My machine is Linux.
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

LVL 48

Expert Comment

ID: 36894877
What Linux distro are you running?

It's very unusual that it would come with GNU grep.
LVL 37

Accepted Solution

Gerwin Jansen earned 250 total points
ID: 36895715
@Tintin - I'm using FC15 - GNU grep 2.8

You may try this awk version if you like:

cut -d"|" -f6 test.txt | awk -- '{for (i=1; i<=NF; i++) if ($i ~ /ORA-[0-9]*/) print $i}'

Open in new window

LVL 37

Expert Comment

by:Gerwin Jansen
ID: 36896285
:) Saw in the result that there is a trailing colon (:) - this one has filtered that:

cut -d"|" -f6 test.txt | awk -- '{for (i=1; i<=NF; i++) if ($i ~ /ORA-[0-9]*/) { sub(/:$/,"",$i) ; print $i } }'

Open in new window


Author Closing Comment

ID: 36903753
This works well on my machine.  Thanks!

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question