Operating System - HP-UX
1843857 Members
2561 Online
110225 Solutions
New Discussion

Re: awk or something else

 
SOLVED
Go to solution
Igor Sovin
Super Advisor

awk or something else

I need to get EP2 and 2006/09/19-3 from
omnidb output:
#omnidb -session -user root -last 3 -type backup -detail

SessionID : 2006/09/19-3
Backup Specification: EP2
Session type : Backup (full)
Started : Tue Sep 19 05:00:00 2006
Finished : Tue Sep 19 09:21:47 2006
Status : Completed
Number of warnings : 0
Number of errors : 0
User : root
Group : sys
Host : lev

How can I do that using awk or some different way?
30 REPLIES 30
Peter Godron
Honored Contributor

Re: awk or something else

Igor,
Using the output of your omnidb command:
head -2 | awk -F':' '{print $2}'
H.Merijn Brand (procura
Honored Contributor

Re: awk or something else

or

# omnidb -session -user root -last 3 -type backup -detail | perl -ne'm/^(SessionID|Backup Specification)\b/&&s/.*?:\s*//&&print'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Igor Sovin
Super Advisor

Re: awk or something else

Hi Peter!

I need to get two fields Session ID an Backup Specification simultaneosly, not only Session ID.
Or if it is not possible, how to get Session ID that belongs to Backup Specification EP2?
The output of omnidb could be different than that I've posted, depending on number of sessions.
Peter Nikitka
Honored Contributor

Re: awk or something else

Hi,

you need pure awk only:
omnidb -session -user root -last 3 -type backup -detail |
awk '/^SessionID/ {print $NF}
/^ Backup Specification/ {print $NF;exit}'

or in Peter G.'s solution:
... | awk -F: 'NR>3 {print $2}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: awk or something else

Sorry,

must use '<' and not '>':

##
or in Peter G.'s solution:
... | awk -F: 'NR<3 {print $2}'
##

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Igor Sovin
Super Advisor

Re: awk or something else

omnidb -session -user root -last 3 -type backup -detail | perl -ne'm/^(SessionID|Backup Specification)\b/&&s/.*?:\s*//&&print '

doesn't work
Igor Sovin
Super Advisor

Re: awk or something else

Actually there are several sessions.
The full output is:

root@lev /opt/omni>omnidb -session -user root -last 3 -type backup -detail

SessionID : 2006/09/16-2
Backup Specification: EP2
Session type : Backup (full)
Started : Sat Sep 16 05:00:01 2006
Finished : Sat Sep 16 09:22:00 2006
Status : Completed
Number of warnings : 0
Number of errors : 0
User : root
Group : sys
Host : lev

SessionID : 2006/09/16-16
Backup Specification: DMProd
Session type : Backup (full)
Started : Sat Sep 16 23:45:00 2006
Finished : Sun Sep 17 02:23:52 2006
Status : Completed
Number of warnings : 1
Number of errors : 0
User : root
Group : sys
Host : lev

SessionID : 2006/09/17-1
Backup Specification: ServiceDesk
Session type : Backup (full)
Started : Sun Sep 17 03:00:01 2006
Finished : Sun Sep 17 04:40:24 2006
Status : Completed
Number of warnings : 0
Number of errors : 0
User : root
Group : sys
Host : lev

SessionID : 2006/09/17-2
Backup Specification: EP2
Session type : Backup (full)
Started : Sun Sep 17 05:00:00 2006
Finished : Sun Sep 17 09:21:42 2006
Status : Completed
Number of warnings : 0
Number of errors : 0
User : root
Group : sys
Host : lev

SessionID : 2006/09/17-14
Backup Specification: DP Database
Session type : Backup (full)
Started : Sun Sep 17 20:00:01 2006
Finished : Sun Sep 17 20:30:17 2006
Status : Completed
Number of warnings : 0
Number of errors : 0
User : root
Group : sys
Host : lev

How to get the following?:

SessionID : 2006/09/16-2
Backup Specification: EP2

SessionID : 2006/09/17-2
Backup Specification: EP2
Peter Godron
Honored Contributor

Re: awk or something else

Igor,
"The output of omnidb could be different than that I've posted, depending on number of sessions." - Can you please post a file with all the formats you want to analyse ?

"I need to get two fields Session ID an Backup Specification simultaneosly, not only Session ID. Or if it is not possible, how to get Session ID that belongs to Backup Specification EP2?" - Can you please give an example of what you want as an output, given the input as requested above.

For example my solution assumed no blank leading line in input and that you wanted the ouput on two lines.

Re: awk or something else

The simpliest way:

omnidb -session -user root -last 3 -type backup -detail | grep -e "^Backup Specification" -e "^SessionID"
H.Merijn Brand (procura
Honored Contributor

Re: awk or something else

Can you expand on "doesn't work"?
Does it give an error message?

Taking your last paste, and using that as input:

lt09:/home/merijn 102 > perl -ne'm/^(SessionID|Backup Specification)\b/&&s/.*?:\s*//&&print' omni.out
2006/09/16-2
EP2
2006/09/16-16
DMProd
2006/09/17-1
ServiceDesk
2006/09/17-2
EP2
2006/09/17-14
DP Database


looks perfectly fine to me.
What is the perl you use?

# perl -v

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Solution

Re: awk or something else

Using awk:

omnidb -session -user root -last 3 -type backup -detail |
awk '/^SessionID/ {print $0}
/^Backup Specification/ {print $0;exit}'
Igor Sovin
Super Advisor

Re: awk or something else

procura

root@lev /Ignite_Bckp>perl -v

This is perl, v5.8.0 built for IA64.ARCHREV_0-thread-multi
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2002, Larry Wall

Binary build 803 provided by ActiveState Corp. http://www.ActiveState.com
Built 16:06:31 Nov 24 2002


Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.

I dont understand how to use your construction.. Could you post the whole command?
Peter Nikitka
Honored Contributor

Re: awk or something else

Hi,

to get the output for multiple sessions, just leave the 'exit'; to get one-liner info additionally, use:

omnidb -session -user root -last 3 -type backup -detail |
awk '/^SessionID/ {id=$NF}
/^ Backup Specification/ {print id,$NF}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Igor Sovin
Super Advisor

Re: awk or something else

Peter Nikitka,

I do not get any output using your last command
H.Merijn Brand (procura
Honored Contributor

Re: awk or something else

Uh, that *is* the whole command, assuming omni.out was the collected output:

root@lev /opt/omni>omnidb -session -user root -last 3 -type backup -detail >omni.out
root@lev /opt/omni>perl -ne'm/^(SessionID|Backup Specification)\b/&&s/.*?:\s*//&&print' omni.out

or, without the file

root@lev /opt/omni>omnidb -session -user root -last 3 -type backup -detail | perl -ne'm/^(SessionID|Backup Specification)\b/&&s/.*?:\s*//&&print' omni.out


I've just tried perl5.8.0 on a HP-UX 10.20 box, and that also runs fine:

d3:/u/usr/merijn 105 > perl5.8.0 -ne'm/^(SessionID|Backup Specification)\b/&&s/.*?:\s*//&&print' omni.out
2006/09/16-2
EP2
2006/09/16-16
DMProd
2006/09/17-1
ServiceDesk
2006/09/17-2
EP2
2006/09/17-14
DP Database

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn

Re: awk or something else

Peter's solution has a blank space that I don't see in my output.

This works for me (without blank space between ^ and Backup):

omnidb -session -user root -last 3 -type backup -detail |
awk '/^SessionID/ {id=$NF}
/^Backup Specification/ {print id,$NF}'
Igor Sovin
Super Advisor

Re: awk or something else

procura
hmm .. dont know what's the problem
I get only SessionID fields:

2006/09/16-2
2006/09/16-16
2006/09/17-1
2006/09/17-2
2006/09/17-14
2006/09/17-17
2006/09/18-2
2006/09/18-19
2006/09/19-1
2006/09/19-3
H.Merijn Brand (procura
Honored Contributor

Re: awk or something else

Which probably means that there is more whitespace than the forum shows.
Can you *attach* the output, instead of paste it?

For the next time you paste data that is to be parsed, please tick the '[ ] Retain format (spacing)' checkbox under the input text box, so the spaces are preserved.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Igor Sovin
Super Advisor

Re: awk or something else

Here is the attachment

Re: awk or something else

the ^ symbol in the logical expressions means "begining of the line".

If you take off the ^ symbol from any of the previous examples you will see it working.

Anyway, it's better if you adjust the expression to the output to avoid false detections.

Re: awk or something else

For example,
This will work:
omnidb -session -user root -last 3 -type backup -detail |
awk '/^SessionID/ {id=$NF}
/Backup Specification:/ {print id,$NF}'

But it will be better if you use this:

omnidb -session -user root -last 3 -type backup -detail |
awk '/^SessionID/ {id=$NF}
/^ Backup Specification:/ {print id,$NF}'
H.Merijn Brand (procura
Honored Contributor

Re: awk or something else

Indeed. Whitespace. It matters :)

# perl -ne'm/^\s*(SessionID|Backup Specification)\b/&&s/.*?:\s*//&&print' omni.out

Should work. An alternate output could be:

perl -ne'm/^SessionID\s*:\s*(.*)/and$sid=$1;if(s/^\s*Backup Specification\s*:\s*//){s/$/ : $sid/;print}' omni.out
EP2 : 2006/09/16-2
DMProd : 2006/09/16-16
ServiceDesk : 2006/09/17-1
EP2 : 2006/09/17-2
DP Database : 2006/09/17-14

Or reverse:

# perl -nle'm/^SessionID\s*:\s*(.*)/and$sid=$1;s/^\s*Backup Specification\s*:\s*//&&print"$sid\t$_"' omni.out
2006/09/16-2 EP2
2006/09/16-16 DMProd
2006/09/17-1 ServiceDesk
2006/09/17-2 EP2
2006/09/17-14 DP Database


Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Nikitka
Honored Contributor

Re: awk or something else

Hi,

ok - awk solution, which deals with multiple spaces:
omnidb -session -user root -last 3 -type backup -detail |
awk '$1 == "SessionID" {id=$NF}
/Backup Specification/ {print id,$NF}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: awk or something else

try the awk construct below:

# awk '{if($0~"EP2") print l"\n"$0; l=$0}' infile

~cheers