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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 36894230
regular expression

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

Expert Comment

by:Gerwin Jansen, EE MVE
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.
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

LVL 48

Expert Comment

ID: 36894877
What Linux distro are you running?

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

Accepted Solution

Gerwin Jansen, EE MVE 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 38

Expert Comment

by:Gerwin Jansen, EE MVE
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

734 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