Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Should I use a do loop and an array?

Status
Not open for further replies.
Dec 12, 2007
19
0
0
US
Backup started at 2008092210050100000
0000 The BACKUP DATABASE command completed successfully.
0001 The BACKUP DATABASE command completed successfully.
0002 The BACKUP DATABASE command completed successfully.
0003 The BACKUP DATABASE command completed successfully.
0004 The BACKUP DATABASE command failed.

Backup was not successful.
Backup ended at 2008092210100200000
--------------------------------------------
If I have the information shown above in a file and want to convert this to 5 records for lines 0000-0004 with the start timestamp and end timestamp at the end of each line, what would be an easy to do this? Should I use a do loop and an array? If so, how?



 
Try this:

Code:
awk '
        /Backup started/ { start=$4 }
        $1 ~ /^[0-9]+$/ { message[++i]=$0 }
        /Backup ended/ {
                end=$4
                for (j=1;j<=i;j++)
                        print message[j],start,end
        }
' inputfile

It accumulates the messages in an array, and when it hits a 'Backup ended', outputs all items in the array.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top