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
753 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

756 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