1832979 Members
3595 Online
110048 Solutions
New Discussion

Re: script help

 
SOLVED
Go to solution
Jeff Paciolla_1
Frequent Advisor

script help

Does anybody have any example of how to parse text by pattern using awk, sed, or grep. I have tried every combination of script that I can think of.

Here is my latest attempt:
test="abcd aa abc : rylfrer01 (xxx.xx.x.xxx) unreachable."

In my script i am trying the following

data=`echo "$test"|sed 's/.*(\: .*\ ).*/\1/'`
echo "$data"

I was hoping this would give me the text rylfrer01

Thanks for your help.
Jeff
10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: script help

What is it you're trying accomplish?
Sridhar Bhaskarla
Honored Contributor

Re: script help

If the example given is the format of your input, then it's easy. Did you try this?

date=`echo $test|awk'{FS=":";print $2}' |awk '{print $1}'`

This will give you rfylfer01.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: script help

Ooops... it's not date..it's data. Replace your sed statement with the statement I gave
by replacing date with data.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor

Re: script help

Hi Jeff:

# echo $test|awk '{print $5}'

Regards!

...JRF...
Jeff Paciolla_1
Frequent Advisor

Re: script help

Patrick - What I am trying to get is the text that starts with "ryl*" from the test message. My dilemma is that the pattern text "ryl*" does not always appear in the same location within the message. So in my script I am trying to parse the ryl* text no matter when or where it appears in the message, and assign that to a different variable.

Thanks
Jeff
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: script help

Okay Jeff, here's my cut at it in awk but it's not real pretty but it will find the ryl string:


TEST="abcd aa abc: rylfrer01 (xxx.xx.x.xxxx) unreachable"

echo ${TEST} | awk '{n = split($0,arry," "); if (n > 0) { i = 1; while (i <= n) {if (match(arry[i],/ryl/) > 0) {printf("%s\n",arry[i])}; ++i }}}'

Clay
If it ain't broke, I can fix that.
Wodisch
Honored Contributor

Re: script help

Hello,

how about plain ol' "cut"?

echo $Test | cut -d':' -f2 | cut -d'(' -f1

Just my ?0.02,
Wodisch
Jeff Paciolla_1
Frequent Advisor

Re: script help

Thanks Clay - This seems to work perfectly

Jeff
Sridhar Bhaskarla
Honored Contributor

Re: script help

Hopefully, I understood your question now.

Try this.

data=`echo $test|sed 's/^.*ryl/ryl/'` |awk "{print $1}'

if you want the entire text after ryl.., take out the awk statement.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Curtis Larson_1
Valued Contributor

Re: script help

echo "$test" | sed 's/.*\(rly.*\)[[:space:]].*/\1/'