Operating System - HP-UX
1831353 Members
2985 Online
110024 Solutions
New Discussion

Another scripting problem

 
SOLVED
Go to solution
Aminur Rizal
Advisor

Another scripting problem

Hi!
This could be a simple problem to you but very complex for me :)
I need to write a script that will grep the content of a program which looks something like this.

pm_9769625.log for details
Wed Oct 2 08:44:17 2002 #9769695# Route status has changed to bad: arc-id=1, Me
ssage:administrative route = APM_X25_BRICKFIELD_U2_SPONT1_CONCHECK See APM log c
6xapm_9769625.log for details
Wed Oct 2 09:28:18 2002 #9769695# Route status has changed to ok: arc-id=1, Mes
sage:administrative route = APM_X25_BRICKFIELD_U2_SPONT1_CONCHECK See APM log c6
xapm_9769625.log for details

============================ Currently Bad Routes ==============================

Sun Oct 6 16:46:32 2002 #9769641# Route status has changed to bad: arc-id=1, Me
ssage:administrative route = APM_X25_YONG_PENG_SPONT1_CONCHECK See APM log c6xap
m_9769625.log for details

How can I grep only the lines after the line "============================ Currently Bad Routes ==============================
"?
Thanks in advance.
Aminur Rizal Afip
16 REPLIES 16
RAC_1
Honored Contributor

Re: Another scripting problem

put everything in file.

sed '/----,$/p' your_file_name
There is no substitute to HARDWORK
Aminur Rizal
Advisor

Re: Another scripting problem

Anil, it doesnt work .
Aminur Rizal Afip
Christian Gebhardt
Honored Contributor

Re: Another scripting problem

Hi

In awk it's like this

awk '{line[NR]=$0;
if ( line[NR] ~ /====== Currently Bad Routes ====/ )
border=NR}
END
{for (i=border+1;i<=NR;i++) print line[i]
}
' filename

Short explanation:
you read the whole file:
line[NR]=$0
remember the line number with the searchpattern
if ( line[NR] ~ /====== Currently Bad Routes ====/ )
border=NR}
and only shows the file from this line to the end
for (i=border+1;i<=NR;i++) print line[i]

Chris
H.Merijn Brand (procura
Honored Contributor

Re: Another scripting problem

# perl -ne '/^=+ Currently Bad/ .. /^$/ and print'
Enjoy, Have FUN! H.Merijn
Brian M Rawlings
Honored Contributor

Re: Another scripting problem

Aminur: interesting problem.

As in all unix scripts, there are no doubt a dozen ways to do this, six of which are acceptable, and a couple of which are ideal.

I think I would start by using a combination of 'wc -l' to determine how many lines total are in the file, and keeping that in a variable.

Then, with 'grep -n ...', grep for the '== Currently Bad Routes ==' string, which would give you the line number for that line. You could create a variable that was the difference between that line and the last line, call it, say, DIFF.

Then, I would use 'tail -$DIFF' on the file, and pipe it to 'grep', which would let you search for whatever you want to match, in just the lines following the "Current Bad Routes" string.

Very possibly, this could all be done in one massive command line, but it would be much simpler to do it as several lines in a script.

I have no machine to hack up a workable version just now, so I'll have to leave it at this, aside from suggesting that the use of 'sed', 'awk', or 'ed' can probably make short work of this, but I have not mastered them enough to suggest exactly how. Then there's perl...

Anyway, I hope these ideas are enough to get you started. Hopefully, they are somewhere in the top six, not the bottom six, of the dozen workable suggestions.

Regards, --bmr
We must indeed all hang together, or, most assuredly, we shall all hang separately. (Benjamin Franklin)
Brian M Rawlings
Honored Contributor

Re: Another scripting problem

Aminur: interesting problem.

As in all unix scripts, there are no doubt a dozen ways to do this, six of which are acceptable, and a couple of which are ideal.

I think I would start by using a combination of 'wc -l' to determine how many lines total are in the file, and keeping that in a variable.

Then, with 'grep -n ...', grep for the '== Currently Bad Routes ==' string, which would give you the line number for that line. You could create a variable that was the difference between that line and the last line, call it, say, DIFF.

Then, I would use 'tail -$DIFF' on the file, and pipe it to 'grep', which would let you search for whatever you want to match, in just the lines following the "Current Bad Routes" string.

Very possibly, this could all be done in one massive command line, but it would be much simpler to do it as several lines in a script.

I have no machine to hack up a workable version just now, so I'll have to leave it at this, aside from suggesting that the use of 'sed', 'awk', or 'ed' can probably make short work of this, but I have not mastered them enough to suggest exactly how. Then there's perl...

Anyway, I hope these ideas are enough to get you started. Hopefully, they are somewhere in the top six, not the bottom six, of the dozen workable suggestions.

Regards, --bmr
We must indeed all hang together, or, most assuredly, we shall all hang separately. (Benjamin Franklin)
H.Merijn Brand (procura
Honored Contributor

Re: Another scripting problem

# perl -ne '/^=+ Currently Bad/ .. /^$/ and print'
Enjoy, Have FUN! H.Merijn
Aminur Rizal
Advisor

Re: Another scripting problem

Chris,
I got the following error:-

syntax error The source line is 5.
The error context is
END >>>
<<<
awk: Quitting
Aminur Rizal Afip
Brian M Rawlings
Honored Contributor

Re: Another scripting problem

Aminur: interesting problem.

As in all unix scripts, there are no doubt a dozen ways to do this, six of which are acceptable, and a couple of which are ideal.

I think I would start by using a combination of 'wc -l' to determine how many lines total are in the file, and keeping that in a variable.

Then, with 'grep -n ...', grep for the '== Currently Bad Routes ==' string, which would give you the line number for that line. You could create a variable that was the difference between that line and the last line, call it, say, DIFF.

Then, I would use 'tail -$DIFF' on the file, and pipe it to 'grep', which would let you search for whatever you want to match, in just the lines following the "Current Bad Routes" string.

Very possibly, this could all be done in one massive command line, but it would be much simpler to do it as several lines in a script.

I have no machine to hack up a workable version just now, so I'll have to leave it at this, aside from suggesting that the use of 'sed', 'awk', or 'ed' can probably make short work of this, but I have not mastered them enough to suggest exactly how. Then there's perl...

Anyway, I hope these ideas are enough to get you started. Hopefully, they are somewhere in the top six, not the bottom six, of the dozen workable suggestions.

Regards, --bmr
We must indeed all hang together, or, most assuredly, we shall all hang separately. (Benjamin Franklin)
Brian M Rawlings
Honored Contributor

Re: Another scripting problem

Holy Cow!

Well, between procura and I, we've used up a drive on the HP web site... Sorry about this, I expect we had the same problem of 'submit' hanging (try again, repeat as necessary...)

If an HP person happens by, maybe they can clean this one up a little.

Oh well, what's a 73GB drive among friends?

8^)
We must indeed all hang together, or, most assuredly, we shall all hang separately. (Benjamin Franklin)
H.Merijn Brand (procura
Honored Contributor

Re: Another scripting problem

Brian, I already reported that to Dan.
Aminur, sorry :)
Enjoy, Have FUN! H.Merijn
Aminur Rizal
Advisor

Re: Another scripting problem

procura,
It doesnt work for me .

Brian,
I agree with your suggestion
For your info the lines are actually output from a troubleshooting script which actually varies with time depends on the errors .
Meant that it is impossible for me to get the number of lines, as it varies.

Still haven't find the right solutions :(
Aminur Rizal Afip
Christian Gebhardt
Honored Contributor
Solution

Re: Another scripting problem

ok. that's a copy and paste problem:

try this:

awk '{line[NR]=$0;
if ( line[NR] ~ /====== Currently Bad Routes ====/ )
border=NR}
END {
for (i=border+1;i<=NR;i++) print line[i]
}
' filename

Chris
Aminur Rizal
Advisor

Re: Another scripting problem

Chris,
Great!!!
It really works this time.

Thanks all, especially for Chris.
You really make my day!
Aminur Rizal Afip
H.Merijn Brand (procura
Honored Contributor

Re: Another scripting problem

Glad that the awk thingy works for you, but just out of curiousity, why does the uttermost simple perl solution not work for you? Still using perl4 from /usr/contrib/bin?
Enjoy, Have FUN! H.Merijn
Steve Post
Trusted Contributor

Re: Another scripting problem

Sed could do it too. Remember sed? Awk's brother?

ANOTHER WAY:
START=`grep -E -n "== Currently Bad Routes ==" yourfile | awk -F: '// {print $1}' `
cat yourfile | sed -n "$START,$p" > your_smaller_file.

another way explained:
Let's say the line you want is line 345.
grep -E -n "== Currently Bad Routes ==" yourfile
Returns 345: == Currently Bad Routes ==
The awk -F: '// {print $1}' gives you the first number (345).

sed -n '345,$p' Says to print only from line 345 to the end (aka $) of the file.
But we use double quotes to interpolate the variable. So
sed -n "$START,$p" becomes sed -n "345,$p"

FYI: I had a problem in the past where the size of the file was so big, I exceeded the ability for tail to get all of the information. Or it was a limit through the pipe "|"? I just remembered that large files had some bizare effects.