Solved

awk in UNIX to split a file based on change in field of header records and group them into files. TRU64 IBM UX

Posted on 2012-03-30
4
749 Views
Last Modified: 2012-04-03
Need an awk or sed command which will split a file based on a change of PO number in the HDR record.


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 a file contains multiple HDR records, grouped on the field beginning in position 47.  Group the HDR and associated DET records together by PO number contained int the field at postion 47.

(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
Comment
Question by:eshapley
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 37789988
awk '/^HDR/{file=substr($0,47,11)}{print >>file}'
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37790150
# same as ozo's solution but more lazy
gawk '/^HDR/{f=$NF}{print > f}'

IIRC, traditional AT&T awk does not support "print >file", hence gawk
0
 

Author Comment

by:eshapley
ID: 37797564
First tried:
/fdmdev/edi-imp>awk `/^HDR/{ProEDI.SHP=substr($0,47,11)}{print>>ProEDI.SHP}`
ksh: 0403-057 Syntax error: `(' is not expected.
/fdmdev/edi-imp>

Next tried:
awk `/^HDR/{ProEDI.DSH=substr($0,47,11)} {print>>/fdmdev/edi-imp/splitfiles1/proedi.dsh1}

error:
ksh: 0403-057 Syntax error: `(' is not expected.

I tried this command:
awk 'BEGIN { i=0 } /^HDR/ {file=substr($0,47,11)} {++i} { print > "/fdmdev/edi-imp/splitfiles1/ProEDI.DSH."(i) }' /fdmdev/edi-imp/ProEDI.DSH

It ran, but created a new file for every record regardless of whether the record began with HDR or DET.  I need it to create a separate file only when the record is HDR and the PO number in that record changes.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 37798597
you used ` where you should have used '
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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.

815 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

7 Experts available now in Live!

Get 1:1 Help Now