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
744 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

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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.

911 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

19 Experts available now in Live!

Get 1:1 Help Now