In a nutshell, I need to work out how to return the last matching pattern from an awk //,// search. I can bring back the first, but am unsure how to obtain the last, and a simple tail won't work as the match could be over multiple lines.
Secondly I would like some way of pattern matching, a pattern matched sequence. The program should work by returning the first section matching /*H,H## from a file, and then performing multiple search's for other tags within (#P,P# #C,C#). I did think of returning the /*H pattern into a variable, and searching that, but that converts it to a string without line breaks (I think), and I want to maintain the format of the input.
The code below works in part (albeit not as described above and the last incorrectly handled #M, M#), but there must be a much more efficient way to do it, as it's searching the full file every time.
Any general advice on optimising the code would also be useful, as I appreciate there's a lot of piping going on.
The current code, input and ouput follow:
#!/bin/ksh
for file in `find . -type f -name "*.code"`
do
echo ">>>>>>> PROGRAM: " $file
echo ================================================================================
awk '/#P/ && ++m==1,/P#/ {print $0}' $file | sed 's/^#[A-Z#] //g' | sed 's/ [A-Z#]#$//g'
awk '/#A/ && ++m==1,/A#/ {print $0}' $file | sed 's/^#[A-Z#] //g' | sed 's/ [A-Z#]#$//g'
awk '/#C/ && ++m==1,/C#/ {print $0}' $file | sed 's/^#[A-Z#] //g' | sed 's/ [A-Z#]#$//g'
awk '/#I/ && ++m==1,/I#/ {print $0}' $file | sed 's/^#[A-Z#] //g' | sed 's/ [A-Z#]#$//g'
echo --------------------------------------------------------------------------------
awk '/#D/ && ++m==1,/D#/ {print $0}' $file | sed 's/^#[A-Z#] //g' | sed 's/ [A-Z#]#$//g'
awk '/#M/,/M#/ {print $0}' $file | tail -1 | sed 's/^#[A-Z#] //g' | sed 's/ [A-Z#]#$//g'
echo
done
/*H#############################################################################
## ##
#P Purpose : Creates generation datasets from the original source file P#
## ##
#A Author : D Turpin A#
#C Date : 1st November 2007 C#
## ##
#I Inputs : data.table1 ##
## data.table2 I#
#O Outputs : data.table3 O#
## ##
################################################################################
## Change History ##
#D Who When Why Version D#
## ##
#M DT 01.11.2007 Initial Development 1.00 M#
#M DT 15.11.2007 Modified to include new reqs 1.01 ##
## and other things M#
## ##
#############################################################################H##
*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+...*/
>>>>>>> PROGRAM: ./header.code
================================================================================
Purpose : Creates generation datasets from the original source file
Author : D Turpin
Date : 1st November 2007
Inputs : data.table1
data.table2
--------------------------------------------------------------------------------
Who When Why Version
and other things
--------------------------------------------------------------------------------