1829403 Members
1935 Online
109991 Solutions
New Discussion

parts of the output?

 
SOLVED
Go to solution
questio
New Member

parts of the output?

This thread has been removed previously??? so please do not delete this thread.

Hi,

line one
line two
line three
line four

I'd like to get "line one" and "line four" only from a log. I don't want to get the lines between them?

10 points for the best answer!

Thanks
8 REPLIES 8
Warren_9
Honored Contributor

Re: parts of the output?


sed '1,4!d' LOGFILE | sed '2,3d'
questio
New Member

Re: parts of the output?

The original question was that to get by strings not by line number. e.g)

> CMD: /dir/example_script 2>&1 /dev/null
> whatever 6827 c Tue Nov 1 10:05:00 EST 2005
< whatever 6827 c Tue Nov 1 10:05:00 EST 2005
< whatever 6821 c Tue Nov 1 10:05:00 EST 2005
< whatever 6823 c Tue Nov 1 10:05:01 EST 2005 rc=126

I'd like to get the "rc=x" and the "CMD" (which is the script that caused the return code)lines. I don't want to get the PID lines between them?

10 points for the best answer!
Hein van den Heuvel
Honored Contributor

Re: parts of the output?

Dear questio,

Welcome to the HPUX forum.

Very few readers here are able to deleted question, and even fewer are in the habit of deleting questions here, no matter how poorly formulated or documented or how poorly the writer indentifies him or herself.

Maybe, just maybe, something went wrong during the 'submit'?

Please help us understand how the script is supposed to recognize the identified lines.
Like whether the word 'four' anywhere on the line is fine, or must is be preceded by line and start the the begin of the line.

Please indicate your preferred tool, if you have one.

This very simple problem can be solve with jsut about all tools: sed, grep, awk, perl, shell scripts.

With grep and the original question:

grep -e "line one|line four" your-file

With awk and your clarification data:

awk '/CMD/;/rc=/' your-file

( if you see CMD then do... the default which is print,
ditto for rc= )

with perl:

perl -ne 'print if /CMD|rc=' your-file

( -n = loop through input but do not print by default
-e = here comes the perl text )

with sed:

sed -n -e "/rc/p" -e"/CMD/p" x

(-n = do not print pattern space be default.
-e = expression
/xyz/ = look for pattern xyz
p = print line on match)

Hope this help some,
Hein.
Hein van den Heuvel
Honored Contributor

Re: parts of the output?


Ooops,

I now see in the 'problem' topic
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=971071
that a question with a poorly choosen subject may have been deleted. I would have replied differently, had I seen that before. Oh well, the perills of 'real time' questions and answers.

Regards,
Hein.
questio
New Member

Re: parts of the output?

Hein,

Here is the output from the awk:

CMD: whatever1.sh 2> /dev/null
< oracle 1003 c Sun Nov 13 19:35:01 EST 2005 rc=126
> CMD: script1.sh > /dev/null 2>&1
> CMD: script2.sh > /dev/null 2>&1
> CMD: script3.sh > /dev/null 2>&1
> CMD: script4.sh > /dev/null 2>&1
> CMD: whatever2.sh 2> /dev/null
< oracle 2003 c Sun Nov 13 19:35:01 EST 2005 rc=126
> CMD: whatever3.sh 2> /dev/null
< oracle 3003 c Sun Nov 13 19:35:01 EST 2005 rc=126

Your solution seemed to grep for all patterns that start with CMD?
Whatever1,whatever2 and whatever3 are correct outputs that I'd like to see but the script1.sh to script4 are unwanted to be seen in the output? since they don't have rc associated with them.

Hope this is more clear.
Thanks
Muthukumar_5
Honored Contributor
Solution

Re: parts of the output?

Use this:

# grep -E '> CMD|rc=' logfile | awk '{ if ($0~"CMD") { ln1=$0;getline;if($0~"rc="){print ln1"\n"$0;}}}'

# cat logfile
> CMD: whatever1.sh 2> /dev/null
< oracle 1003 c Sun Nov 13 19:35:01 EST 2005 rc=126
> CMD: script1.sh > /dev/null 2>&1
> CMD: script2.sh > /dev/null 2>&1
> CMD: script3.sh > /dev/null:2>&1
> CMD: script4.sh > /dev/null 2>&1
> CMD: whatever2.sh 2> /dev/null
< oracle 2003 c Sun Nov 13 19:35:01 EST 2005 rc=126
> CMD: whatever3.sh 2> /dev/null
< oracle 3003 c Sun Nov 13 19:35:01 EST 2005 rc=126

# grep -E '> CMD|rc=' logfile | awk '{ if ($0~"CMD") { ln1=$0;getline;if($0~"rc="){print ln1"\n"$0;}}}'
> CMD: whatever1.sh 2> /dev/null
< oracle 1003 c Sun Nov 13 19:35:01 EST 2005 rc=126
> CMD: whatever2.sh 2> /dev/null
< oracle 2003 c Sun Nov 13 19:35:01 EST 2005 rc=126
> CMD: whatever3.sh 2> /dev/null
< oracle 3003 c Sun Nov 13 19:35:01 EST 2005 rc=126

hth.
Easy to suggest when don't know about the problem!
questio
New Member

Re: parts of the output?

Excellent Kumar!!!
10 points for you sir!

Just a quick note:

grep -E '> CMD|rc=' logfile

I removed the ">" so the line looks like this:

grep -E 'CMD|rc=' logfile

it worked like a charm!

Thanks again
questio
New Member

Re: parts of the output?

.