1846563 Members
1791 Online
110256 Solutions
New Discussion

Parse text in script

 
SOLVED
Go to solution
Jeff Paciolla_1
Frequent Advisor

Parse text in script

Can anyone tell me how to return text from a log file leaving certain characters out.

Example Log File
abcd aa abcd (xxxx)

What I want to return from this entry in the log using a script is
xxxx

I don't want the parenthesis. I have been trying to do this using an awk statement, but I seem to be leaving something out.

All help is appreciated
Thanks
Jeff
15 REPLIES 15

Re: Parse text in script

Jeff,

Have you tried to use single quotes?

Rgds,

Pieter
Jeff Paciolla_1
Frequent Advisor

Re: Parse text in script

i am not sure exactly what you mean by using single quotes, but I will provide a little info about the log file. It comes to me when an event takes places in my NNM network. I have no control over the format of the text message so I can't change the look. I just have to grab the info inside the ()

Thanks for your help
Bruce Bush
New Member

Re: Parse text in script

Greetings Jeff,

This pipeline is one way to do it :

cat logfile | tr -d "()" | awk '{ print $4 }'

The "tr -d" deletes whatever characters are between the double quotes.

Regards,
Bruce




A. Clay Stephenson
Acclaimed Contributor

Re: Parse text in script

Hi Jeff:

Create an awk script like this one (I've intentionally made it more verbose than necessary for clarity) called my.awk:

{
n1 = index($0,"(")
if (n1 > 0)
{
n2 = index($0,")")
if (n2 > n1)
{
s = substr($0,n1 + 1,n2 - n1 -1)
printf("%s\n",s)
}
}
}

cat yourfile | awk -f my.awk

That should do it, Clay
If it ain't broke, I can fix that.
Richard Darling
Trusted Contributor

Re: Parse text in script

Hi Jeff,
You could do it with:

root:L1000>:awk '{print $3}' test | sed 's/)//g' | sed 's/(//g' > final

probably prettier ways to do it, but this would be quick....
RD
Richard Darling
Trusted Contributor

Re: Parse text in script

Jeff, "test" would be your logfile...
RD
Bruce Bush
New Member

Re: Parse text in script

Hi Jeff,

Yet another variation in awk would be...

cat logfile | awk '{ sub(/\(/, ""); sub(/\)/, ""); print $4 }'

Enjoy,
Bruce
Robin Wakefield
Honored Contributor
Solution

Re: Parse text in script

Hi Jeff,

Using sed:

$ echo 'abcd aa abcd (xxxx)' | sed 's/.*(\(.*\)).*/\1/'
xxxx

Rgds, Robin.
Jeff Paciolla_1
Frequent Advisor

Re: Parse text in script

Thanks to everybody that replied, I decided on going with the sed command. It worked great.

Jeff
Curtis Larson_1
Valued Contributor

Re: Parse text in script

cat file | awk -F[\(\)] '{print $2;}'
Curtis Larson_1
Valued Contributor

Re: Parse text in script

Now if all the lines aren't formated as stated:

you'll probably have to add a NF>2 as in
'NF>2 {print $2;}'

this still doesn't in sure that there is a ( ) pair. i.e. two ) or two ( on a line will still generate output. For this situation I'd use the sed suggestion also because it matches a more "exact" pattern. i.e. a () pair. Although it will only output the pattern in the last () pair on a line if there is more then one.
Robin Wakefield
Honored Contributor

Re: Parse text in script

Hi Jeff,

My reply was for processing a single line. For processing files, use:

sed -n 's/.*(\(.*\)).*/\1/p' file

to *only* display the bracketed contents within the file.

Rgds, Robin.
Jeff Paciolla_1
Frequent Advisor

Re: Parse text in script

i appreciate everybody's help on this issue. Just one more question, shouldn't I be able to do the same thing with a NNM variable as opposed to a logfile.
Example:
instead of logfile being
abcd aa abcd (xxxx)
what if
$1 from a NNM alert was

abcd aa abcd (xxxx)

Could I do something like this

Test=$1 | `awk '{print $4}'`

Where I want to set variable Test equal to xxxx

Thanks
Jeff
James R. Ferguson
Acclaimed Contributor

Re: Parse text in script

Hi Jeff:

# X=`echo "abcd aa abcd (xxxx)"|awk '{print $4}'`
# echo $X

...would yield "(xxxx)"

Similarly, leveraging Robin's first solution:

# Y=`echo 'abcd aa abcd (xxxx)' | sed 's/.*(\(.*\)).*/\1/'`
# echo $Y

...would yield "xxxx"

Regards!

...JRF...
Jeff Paciolla_1
Frequent Advisor

Re: Parse text in script

Thanks James that worked perfectly

Jeff