Getting lines from a report - ksh (304 Views)
Reply
Super Advisor
Manuales
Posts: 937
Registered: ‎11-18-2005
Message 1 of 8 (304 Views)
Accepted Solution

Getting lines from a report - ksh

I have this report, the space can be appear after 1,2,3 or more lines with information, in this example the
first Job has 5 lines of info, the, appears the space, the following job has only 1 line of info then
appears an space and go on ....

Report 07 Job History Listing 06/02/10
Date Time Schedule Name Elapsed CPU Status

line 1: Job:PDWH06UX #PPYHP_DWH_ECS_NACRLAN_M
line 2: 06/01/10 15:05 PDWH06UX #JOBS 1 1
line 3: 06/01/10 15:33 PDWH06UX #JOBS2 1 1
line 4: 06/01/10 15:34 PDWH06UX #JOBS3 1 1
line 5: 06/01/10 15:36 PDWH06UX #JOBS4 1 1
line 6: 06/01/10 15:37 PDWH06UX #JOBS5 1 1
line 7:
line 8: Job:PDWH06UX #PPYHP_DWH_ECS_QUL1_D
line 9: 06/01/10 06:40 PDWH06UX #PPYHP_DWH_ECS_D 1 1
line 10:
line 11: Job:PDWH06UX #PPYHP_DWH_ECS_PYJFPLOG_D
line 12: 06/01/10 06:38 PDWH06UX #PPYHP_DWH_ECS_D 1 1
line 13: 06/01/10 07:28 PDWH06UX #PPYHP_DWH_ECS_D2 1 1
line 14: 06/01/10 15:32 PDWH06UX #JOBS8 1 1
line 15:

i need to get a report as follows, first get the first line "Report 07 bla bla bla ", then to get the lline that says "Job" and getting the last line before the space ...
and the get again the line which says "job" and then to get the line before the space as follows
Report 07 Job History Listing 06/02/10
Date Schedule Name Elapsed
line 1: Job:PDWH06UX #PPYHP_DWH_ECS_NACRLAN_M
line 6: 06/01/10 PDWH06UX #JOBS5
line 7:(space)
line 8: Job:PDWH06UX #PPYHP_DWH_ECS_QUL1_D
line 9: 06/01/10 PDWH06UX #PPYHP_DWH_ECS_D
line 10: (space)
line 11: Job:PDWH06UX #PPYHP_DWH_ECS_PYJFPLOG_D
line 14: 06/01/10 PDWH06UX #JOBS8

Thanks in advance.
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 2 of 8 (304 Views)

Re: Getting lines from a report - ksh

> i need to get a report [...]

Are you getting paid to do this?

Have you considered getting a book, or taking
a class, to learn how to write a computer
program?
Super Advisor
Manuales
Posts: 937
Registered: ‎11-18-2005
Message 3 of 8 (304 Views)

Re: Getting lines from a report - ksh

Hey .. this is part of my work.
I have done shells in ksh , i have used a little bit the awk but i do not know how to evaluate "if there is an space, keep previous line in a variable"

can you help me with this:
"if there is an space, keep previous line in a variable"
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 4 of 8 (304 Views)

Re: Getting lines from a report - ksh

Steven, you gotta admit it was a much more clear problem description then most. That's a much appreciated step 1.

Manuales...

You about this awk 'one liner' or mini program:

awk '(NR < 3)
/^Job:/ { job = $0 }
!NF && job != "" { print; print job; print last; job="" }
{ last = $1 " " $3 " " $4}' report.txt


In slo mo...

(NR < 3) : true for the first 2 lines. No action specified, so take default action is to print the input line.


/^Job:/ { job = $0 } : If the line starts with Job: then save it in variable job

!NF && job != "" { print; print job; print last; job="" } : If there are NO fields on the line, and the variable job is not empty, then print a blank line, print job, and print a variable last which will hold data from the last non-blank line.

{ last = $1 " " $3 " " $4} : any time, unconditionally, stash field 1, 3 and 4 in a variable last, just in case it really is the last line.


Enjoy,
Hein


Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 5 of 8 (304 Views)

Re: Getting lines from a report - ksh

forgot to add...

The test for job != "" and the clearing of job after print is in order to protect against additional blank lines or random blob of text in the report.

if the input is exactly as you describe, then this will be enough:

$ awk 'NR < 3; /^Job:/ {print "\n" $0}; !NF {print last} { last = $0}' report
Report 07 Job History Listing 06/02/10
Date Time Schedule Name Elapsed CPU Status

Job:PDWH06UX #PPYHP_DWH_ECS_NACRLAN_M
06/01/10 15:37 PDWH06UX #JOBS5 1 1

Job:PDWH06UX #PPYHP_DWH_ECS_QUL1_D
06/01/10 06:40 PDWH06UX #PPYHP_DWH_ECS_D 1 1

Job:PDWH06UX #PPYHP_DWH_ECS_PYJFPLOG_D
06/01/10 15:32 PDWH06UX #JOBS8 1 1

Hein

Super Advisor
Manuales
Posts: 937
Registered: ‎11-18-2005
Message 6 of 8 (304 Views)

Re: Getting lines from a report - ksh

Hi Hein, thank you so much ......
let me try right now, let me test it .... :)
Super Advisor
Manuales
Posts: 937
Registered: ‎11-18-2005
Message 7 of 8 (304 Views)

Re: Getting lines from a report - ksh

Hein, really good explanation, thanks a lot for your GREAT support !!!!!!

i was thinking in using a "for" to go evaluating line per line but i see here that i have to study more about AWK ..


really thanks again !!!! :) IT worked successfully :).
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 8 (304 Views)

Re: Getting lines from a report - ksh

Hi Manauales:

Here's another way:

# perl -00 -ne '/^Report/ and print,next;/(^Job.+?$)(.*)(^\d.+$)/sm and print $1,"\n",$3' file

This reads "paragraphs" of data at a time (-00). If a paragraph begins wth "Report", print it in its entirety. If a paragraph begins with "Job", then capture the string until a newline in $1 and don't be greedy about it; grab zero or more characters in $2; and lastly stuff a line that begins with a digit (\d) into $3. If we can make such a match, print what we captured in $1 and $3.

Since we don't care about $2 (the lines beginning with a digit that aren't the last one in a group) we could/should skip their capture. To do this we make a slight change and tell the regular expression engine that we don't want to capture (?:) the second group ().

# perl -00 -ne '/^Report/ and print,next;/(^Job.+?$)(?:.*)(^\d.+$)/sm and print $1,"\n",$2' file

Regards!

...JRF...
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.