Solved

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

Posted on 2012-03-28
5
777 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…

743 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

12 Experts available now in Live!

Get 1:1 Help Now