Solved

awk in UNIX to split a file based on the header records. TRU64 IBM UX

Posted on 2012-03-28
5
785 Views
Last Modified: 2012-06-27
The file has multiple header records in it.  I need to split the HDR and the records up to the next HDR into a separate file.  For example if there are three records which have HDR850 in the first six positions then I would split the file into three separate files.  

Basically the record structure looks like:
HDR850
DET0001
DET0002
DET0003
HDR850
DET0001
DET0002
DET0003
HDR850
DET0001
DET0002
DET0003
DET0004

I currently have an awk script which splits the file up on record count.  Maybe we could just modify this a little:

Calling the awk:

awk -f split.awk /fdmdev/edi-exp/MEIJR00.846

split.awk commands:

BEGIN {
x = 0
y = 1
     }
{
filename = "/fdmdev/edi-exp/catalog/MEIJR00."y".846"
if ( x == 0 ) header = $0
if ( x == 0 ) {
 x++
 next
 }
if ( x == 1 ) printf("%s\n",header) > filename
if ( x++ < 8999 ) printf("%s\n",$0) >> filename
else {
printf("%s\n",$0) >> filename
y++
x = 1
}
}
0
Comment
Question by:eshapley
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:johnsone
ID: 37779162
This should do it

awk 'BEGIN { i=0 } /^HDR/ {++i} { print > (i)".out"}' <filename>

Open in new window


You will get files call 1.out, 2.out, 3.out, etc.
0
 

Author Comment

by:eshapley
ID: 37782671
This looks like something I can use. I discovered while developing this solution that I need to change the requirements a little.  I actually need to split to a new file each time this key value changes, beginning at position 1 of the same records:
HDR850                              VPD        C20731589.

For example:

HDR850                              VPD        C20731589
DET0001
DET0002
DET0003
HDR850                              VPD        C20731590
DET0001
DET0002
DET0003
HDR850                              VPD        C20731591
DET0001
DET0002
DET0003
0
 

Author Comment

by:eshapley
ID: 37782839
Error with suggested line:
/usr/local/cron/edi>awk 'BEGIN { i=0 } /^HDR/ {++i} { print > "ProEDI.DSH."(i)" }' /fdmdev/edi-imp/ProEDI.DSH
 Syntax Error The source line is 1.
 The error context is
                BEGIN { i=0 } /^HDR/ {++i} { print > "ProEDI.DSH."(i)" >>>  } <<< 
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
        awk: 0602-540 There is a missing } character.
0
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 37782948
You have only 3 quotes.  They need to be matched.  Probably more like this:

awk 'BEGIN { i=0 } /^HDR/ {++i} { print > "ProEDI.DSH."(i) }' /fdmdev/edi-imp/ProEDI.DSH

Open in new window

0
 

Author Comment

by:eshapley
ID: 37784061
Great.  I still need help with the revised HDR record criteria.  Can you suggest how to split on that?

For Example:

If /^HDR/ evaluate expression from position 47 through 57.  
     If expression in 47 through 57 is unchanged when compared with last HDR, keep record with the last HDR850 and DET grouping.
     If expression in 47 through 57 changes from last HDR split to a new file.

For example:
(file1)
HDR850                              VPD        C20731589
DET0001
DET0002
DET0003
HDR850                              VPD        C20731589
DET0001
DET0002
DET0003
(file2)
HDR850                              VPD        C20731590
DET0001
DET0002
DET0003
(file3)
HDR850                              VPD        C20731591
DET0001
DET0002
DET0003
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now