Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
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
Medium Priority
?
766 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 2000 total points
ID: 37798597
you used ` where you should have used '
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses

647 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