Operating System - HP-UX
1752618 Members
4384 Online
108788 Solutions
New Discussion юеВ

Getting lines from a report - ksh

 
SOLVED
Go to solution
Manuales
Super Advisor

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.
7 REPLIES 7
Steven Schweda
Honored Contributor

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?
Manuales
Super Advisor

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"
Hein van den Heuvel
Honored Contributor
Solution

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


Hein van den Heuvel
Honored Contributor

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

Manuales
Super Advisor

Re: Getting lines from a report - ksh

Hi Hein, thank you so much ......
let me try right now, let me test it .... :)
Manuales
Super Advisor

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 :).
James R. Ferguson
Acclaimed Contributor

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...