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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

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

757 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