Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to split an EDI file which contents a lot off invoices in two files.
The end of invoice segments has two different strings (text):
1- “FTX Credit note retro active price adjustments” or
2- “FTX Self-billed invoices”Also the result will be File1=> all Invoices with string Nr.1
File2=> all invoices segments with string Nr.2
The main file look like that://ODETT//INVGM
UNB 090114
MID 00003456 090114
SDT012837259
…..
……
FTX Credit note retro active price adjustments/ODETT//INVGM
UNB 090114
MID 00001918 090114
SDT012837259
……
……
FTX Self-billed invoices//ODETT//INVGM
UNB 090107
MID 00001222 090107
SDT012837259
……
……
FTX Self-billed invoices//ODETT//INVGM
UNB 090115
MID 00004180 090115
SDT012837259
…….
…….
FTX Credit note retro active price adjustments…..
…..
And so on.Thanks in advance

This script contains two passes. The first pass is an awk script that determines whether an invoice belongs to File1, File2, or sent to the null device. (I'm using nawk so you might have to change to awk):
#!/bin/ksh # two pass method. Make a pass thru the data file and determine whether the # invoice is "credit note" or "self-billed" rm -f intradfile2 File1 File2 nawk ' { if($1 == "FTX") # print once per invoice if($2 ~ /Credit*/) print "1" else if($2 ~ /Self-billed*/) print "2" else print "S" # skip }' datafile > intradfile2 # Read the intermediate file into an array. cycle thru the # main file, keep a running count of the number of invoices and # check the array to determine if the invoice belongs in File1 or File2 or /dev/null nawk ' BEGIN { tot=0; cnt=0 # read the file into the array while ( getline line < "intradfile2" > 0 ) { cnt++ n[cnt]=line } } { if($0 ~ /\/\/ODETT\/\/INVGM/) { tot++ prcnt=n[tot] } if(prcnt=="1") print $0 >> "File1" if(prcnt=="2") print $0 >> "File2" if(prcnt=="S") print $0 >> "/dev/null" }' datafile

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |