klut
April 15, 2008, 10:12am
1
I am using HP UX and think this may be done with awk but bot sure.
I have a file with a several header records and undeneath many detail records I need to put in the header record the number of detail records above this header record and number of detail records below this header record
Header records are identified by the line beiginning with H
Detail records are identified with the line beginning with I
e.g
Hxxxxxxxx004xxxxxxxxxx000 (004 records below header record 0 above
I
I
I
I
Hxxxxxxxx003xxxxxxxxxx004 (003 record below this header record 4 above)
I
I
I
aigles
April 15, 2008, 11:40am
2
Try (and adapt) the following awk program :
awk '
function dump_block() {
if (count == 0) return;
hdr = sprintf("%s%03.3d%s%03.3d%s", substr(record[0], 1, 9), below,
substr(record[0], 13, 10), above,
substr(record[0], 26 ));
print hdr;
for (i=1; i<=below; i++) {
print record;
delete record;
}
}
/^H/ {
dump_block()
record[0] = $0;
count++;
above = below;
below = 0;
next;
}
{
record[++below] = $0;
}
END {
dump_block()
}
' inputfile > outputfile
Jean-Pierre.
Can you provide a better sample of input data?
klut
April 16, 2008, 3:42am
4
Thanks for that here is a more detailed sample set
Detailed Sample Data Set
More detailed sample data set
4th Field of H record = Number of I records before this record
5th Field of H record = Number of I records after this record
H|SHCDL|0712|000000|000006
I|SHCDL22|A|D|93XXXX446|XRZ|FRZ|071222|0001|00000.50
I|SHCDL23|A|C|93XXXX446|DRZ|FRB|071223|0032|00000.75
I|SHCDL24|A|X|93XXXX446|VRZ|FRC|071224|0456|00000.99
I|SHCDL25|A|L|93XXXX46|LRZ|FRY|071225|0333|00000.79
I|SHCDL26|B|D|93XXXX446|IRZ|FRX|071226|0221|00000.90
I|SHCDL27|B|C|9XXXX6|XRZ|FRL|071227|0001|00000.34
H|SHYDL|0711|000006|000004
I|SHYDL22|A|X|93XXXX4|XYZ|FYZ|071110|0001|00000.40
I|SHYDL23|C|L|93XXXX444|DYZ|FYB|071111|0022|00000.35
I|SHYDL24|A|X|93XXXX4|VYZ|FYC|071112|0456|00000.29
I|SHYDL25|C|T|9XXXX44|LYZ|FYY|071113|0132|00000.69
aigles
April 16, 2008, 6:05am
5
A new version of my awk program :
awk '
function dump_block() {
if (count == 0) return;
hdr = record[0];
sub("-above-", sprintf("%06.6d", above), hdr);
sub("-below-", sprintf("%06.6d", below), hdr);
print hdr;
for (i=1; i<=below; i++) {
print record;
delete record;
}
}
BEGIN {
FS = OFS = "|";
}
/^H/ {
dump_block()
$4 = "-above-"
$5 = "-below-"
record[0] = $0;
count++;
above = below;
below = 0;
next;
}
{
record[++below] = $0;
}
END {
dump_block()
}
' inputfile > outputfile
Input file:
H|SHCDL|0712|aaaaaa|bbbbbb
I|SHCDL22|A|D|93XXXX446|XRZ|FRZ|071222|0001|00000.50
I|SHCDL23|A|C|93XXXX446|DRZ|FRB|071223|0032|00000.75
I|SHCDL24|A|X|93XXXX446|VRZ|FRC|071224|0456|00000.99
I|SHCDL25|A|L|93XXXX46|LRZ|FRY|071225|0333|00000.79
I|SHCDL26|B|D|93XXXX446|IRZ|FRX|071226|0221|00000.90
I|SHCDL27|B|C|9XXXX6|XRZ|FRL|071227|0001|00000.34
H|SHYDL|0711|xxxxxx|yyyyyy
I|SHYDL22|A|X|93XXXX4|XYZ|FYZ|071110|0001|00000.40
I|SHYDL23|C|L|93XXXX444|DYZ|FYB|071111|0022|00000.35
I|SHYDL24|A|X|93XXXX4|VYZ|FYC|071112|0456|00000.29
I|SHYDL25|C|T|9XXXX44|LYZ|FYY|071113|0132|00000.69
Output file:
H|SHCDL|0712|000000|000006
I|SHCDL22|A|D|93XXXX446|XRZ|FRZ|071222|0001|00000.50
I|SHCDL23|A|C|93XXXX446|DRZ|FRB|071223|0032|00000.75
I|SHCDL24|A|X|93XXXX446|VRZ|FRC|071224|0456|00000.99
I|SHCDL25|A|L|93XXXX46|LRZ|FRY|071225|0333|00000.79
I|SHCDL26|B|D|93XXXX446|IRZ|FRX|071226|0221|00000.90
I|SHCDL27|B|C|9XXXX6|XRZ|FRL|071227|0001|00000.34
H|SHYDL|0711|000006|000004
I|SHYDL22|A|X|93XXXX4|XYZ|FYZ|071110|0001|00000.40
I|SHYDL23|C|L|93XXXX444|DYZ|FYB|071111|0022|00000.35
I|SHYDL24|A|X|93XXXX4|VYZ|FYC|071112|0456|00000.29
I|SHYDL25|C|T|9XXXX44|LYZ|FYY|071113|0132|00000.69
Jean-Pierre.
klut
April 16, 2008, 9:04am
6
Cheers Jean-Pierre et al,
I owe you one