Visit Our UNIX and Linux User Community


awk script to update header record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to update header record
# 1  
Old 04-15-2008
awk script to update header record

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

Last edited by klut; 04-15-2008 at 11:24 AM..
# 2  
Old 04-15-2008
Try (and adapt) the following awk program :

Code:
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[i];
      delete record[i];
   }
}
/^H/ {
   dump_block()
   record[0] = $0;
   count++;
   above = below;
   below = 0;
   next;
}
{
   record[++below] = $0;
}
END {
   dump_block()
}
    ' inputfile > outputfile

Jean-Pierre.
# 3  
Old 04-15-2008
Question Sample input data

Can you provide a better sample of input data?
# 4  
Old 04-16-2008
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
# 5  
Old 04-16-2008
A new version of my awk program :
Code:
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[i];
      delete record[i];
   }
}
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:
Code:
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:
Code:
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.
# 6  
Old 04-16-2008
Cheers Jean-Pierre et al,

I owe you one

Previous Thread | Next Thread
Test Your Knowledge in Computers #2
Difficulty: Easy
Unix was invented by Steve Jobs of Apple Computer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - How to update header of scripts in one pass - multiline search/replace

Hello. A find command return a list of file. For each fileReplace the content starting with the first "§" (of two) ending with last "ɸ" (of two), regardless of the content ( five lines ) by the following content (exactly) : §2019_08_23§ # # ... (8 Replies)
Discussion started by: jcdole
8 Replies

2. UNIX for Beginners Questions & Answers

Help in printing records where there is a 'header' in the first record ???

Hi, I have a backup report that unfortunately has some kind of hanging indent thing where the first line contains one column more than the others I managed to get the output that I wanted using awk, but just wanting to know if there is short way of doing it using the same awk Below is what... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Programming

MYSQL - trigger update on record insert or update

Right I have a MYSQL database with table1 with 3 columns, colA, colB and colC. I want to combine the data in the 3 columns into a 4th column names col_comb. Here's the SQL command that works: UPDATE table1 SET `col_comb` = CONCAT( `colA` , ' - ', `colB` , ', ', `colC` ); So now I want this... (5 Replies)
Discussion started by: barrydocks
5 Replies

4. UNIX and Linux Applications

Script to delete few rows from a file and then update header

HJKL1Name00014300010800000418828124201 L201207022012070228XAM 00000000031795404 001372339540000000000000000000000 COOLTV KEYA Zx00 xI-50352202553 00000000 00000000 G000000000000 00000000 ... (10 Replies)
Discussion started by: mirwasim
10 Replies

5. Shell Programming and Scripting

Approach on Header record

All, I currently have a requirement to fetch a Date value from a table. And then insert a Header record into a file along with that date value. ex: echo "HDR"" "`date +%Y%j` `date +%Y%m%d` In the above example I used julian date and standard date using Current Date. But the requirement... (0 Replies)
Discussion started by: cmaroju
0 Replies

6. Shell Programming and Scripting

how to exclude the header in shell script using awk

Hello Everyone In my shell script, I am retrieving the cluster ID and node number of an LPAR using the following command - lsclcfg -l This command's output looks as follows - CLUSTER_NAME CLUSTER_ID NODE_NR sch1h004 6104567 3 I want to store only the... (3 Replies)
Discussion started by: gates1580
3 Replies

7. Shell Programming and Scripting

Insertion of Header record

A header record is to be inserted in the begining of a flat file without using extra file or new file. It should be inserted into same file. Advace thanks for all help... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

8. Shell Programming and Scripting

Skip parsing the header record - Awk

Guys.... Got a scenario in which I need to skip parsing the header record while I do an awk. Does awk has the flexibility to accomplish this?. If so, how do we do this?. Thanks !!! -Anduzzi :) (2 Replies)
Discussion started by: anduzzi
2 Replies

9. UNIX for Dummies Questions & Answers

change order of fields in header record

Hello, after 9 months of archiving 1000 files, now, i need to change the order of fields in the header record. some very large, space padded files. HEADERCAS05212008D0210DOMEST01(spacepadded to record length 210) must now be 05212008HEADERCASD0210DOMEST01(spacepadded to record length 210) ... (1 Reply)
Discussion started by: JohnMario
1 Replies

10. UNIX for Dummies Questions & Answers

How to extract duplicate records with associated header record

All, I have a task to search through several hundred files and extract duplicate detail records and keep them grouped with their header record. If no duplicate detail record exists, don't pull the header. For example, an input file could look like this: input.txt HA D1 D2 D2 D3 D4 D4... (17 Replies)
Discussion started by: run_eim
17 Replies

Featured Tech Videos