Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a basic Unix knowledge.
I need a script to check and edit a big Invoice file(EDI Message).
First the script shall make a small ajustment to the SDT segment.We need to add an ‘A’ at the end of the SDT segment for all flows which don’t denote POP shipments (POP shipments easy to recognize by despatch number which is the RAD number in the EDI message : RAD POP090107). The begining of the file look like follow://ODETT//INVGM
UNB 090114
MID 00001918 090114
SDT012837259
BDT01315000737
PAI EUR 090202
RAD 37062241 081223
CSG01184
CSG02SDT012837259 to be changed into SDT012837259A when The "RAD" number without "POP" at the begining.
Second this big message shall be splittet in two files at a special position.
Please let me know if you need more information.Thnaks in advance

I can not give you a complete solution because you don't tell what starts the invoice and what ends it. You need to post more of your data file.
The following sed command adds an "A" to the end of any line (which has optional spaces) that starts with the SDT
sed '/^SDT/s/[ ]*$/A/' datafile.txt
The second part of your requirement is more difficult. The way I read it is that you don't want to append the "A" if a string starting with "RAD" is 3 lines below the line starting with SDT.
In shell, this is difficult to do since you have to read ahead of the file until you hit the "RAD" string or the end of the invoice.
Personally, I think it's easier to reverse the file so you read the "RAD" string first which tells you whether or not to add the "A".
Of course, when the processing is done, reverse the file.
That's the best I can do until you reply.

Hi Nails,
the begining of this file look like that:
//ODETT//INVGM
UNB 090106
MID 00000047 090106
SDT012837259
BDT01315000737
PAI EUR 090106
RAD POP081204 081204
CSG0172261
CSG02
ARD0113268023
PRI 000000001NTP000000000000000
ARD0113268176
PRI 000000001NTP000000000000000
.....
.....
//ODETT//INVGM
UNB 090106
MID 00000046 090106
SDT012837259
BDT01315000737
PAI EUR 090106
RAD 000081113 081113
CSG0172261
CSG02
ARD0113268019
PRI 000000001NTP000000000000000
ARD0113268022
PRI 000000001NTP000000000000000
RAD POP081114 081114
CSG0172261
CSG02
ARD0113268186
.....
..... and so on till the end of the file.If you need the complete file I can provide it to you.
Like you see there is alot off SDT and RAD segments. But like I told you we need to change(edit) the SDT number only when the "RAD" string number(3 lines below)do not content the "POP" segment at the begining.
You're right it is may be simple if we beginn to read first the RAD string.
Any replay is welcome.
Thanks in advance.
Fouad

I have some questions:
1) I'm assuming this string marks the start of an invoice:
//ODETT//INVGM
2) How big is the data file, how many invoices typically?
3) Is this statement correct:
DO NOT add an "A" to the end of the "SDT" string if the invoice contains a string starting with "RAD"

to 1) it's true
to 2) ~24 invoices(sometimes less and sometimes more)
to 3) not exactely: DO NOT add an "A" to the end of the "SDT" string if the number after the "RAD" string (3 lines below) do content the "POP" segment at the begining. Example
-RAD POP081204 081204--> DO NOT
-RAD 000081113 081113 --> DO

As I said, I reversed the invoice file so I could sample the "RAD" & "RAD POP" lines before processing the "SDT" line. Obviously, this won't work if the order is reversed.
I used perl to reverse the file and then reverse it back once processing is complete. If you don't want to use perl, check out this article for other reverse options:
http://www.samag.com/documents/s=97...
newdatafile file contains the desired output.
#!/bin/ksh rm -f intdatafile intdatafile2 perl -we 'print reverse <>' datafile > intdatafile newinv=0 while read line do case $line in \/\/ODETT\/\/INVGM*) newinv=0;; esac case "$line" in RAD*) newinv=1;; esac case "$line" in "RAD POP"*) newinv=0;; esac if [[ $newinv -eq 1 ]] then case "$line" in SDT*) newline="$line"A;; *) newline="$line";; esac else newline="$line" fi echo "$newline" done < intdatafile > intdatafile2 # reverse back perl -we 'print reverse <>' intdatafile2 > newdatafile

Hi Nails,
I used "awk" to to reverse the file and then reverse it back once processing is complete and it is working.
But he result was not correct because this scripts added an "A" to the end of every "SDT" string aside from the "RAD" segment number type(with POP or without).
Have you an idea what is going wrong?Many thanks

I went back and checked my work and it's correct. I'm using Solaris 9 where you are using AIX.
I'm assuming that you copied my script exactly so it looks like the comparisons in the case statements aren't working.
You can post the code you are using and I'll look at it, but I'm assuming it's the same.
I can rewrite this in awk, but I can't get to it until tonight.
Then, again, what about your data? I'm looking for "RAD POP". Do you have something like "RAD POP"?

Like you say I did not change the script only by the file reverse and back. The script looks like follow:
#!/bin/ksh
#reverse the file
awk '{ a[NR]=$0 } END { for(i=NR; i; --i) print a[i] } ' indatafile_full > indatafile_full2#Make the needed change when it is necessary
newinv=0
while read line
do
case $line in
FTX*)
newinv=0;;
esaccase "$line" in
RAD*)
newinv=1;;
esaccase "$line" in
"RAD POP"*)
newinv=0;;
esacif [[ $newinv -eq 1 ]]
then
case "$line" in
SDT*)
newline="$line"A;;
*)
newline="$line";;
esac
else
newline="$line"
fi
echo "$newline"
done < indatafile_full2 > indatafile_full3
# reverse back
awk '{ a[NR]=$0 } END { for(i=NR; i; --i) print a[i] } ' indatafile_full3 > newdatafile_4I have also changed the begining of the file:
from \/\/ODETT\/\/INVGM*) to FTX*)because when the file is inversed it begins with the "FTX" string.The big file contents two differents RAD strings; for exemple somthing like this:
RAD 0000000055363 081208
or
RAD POP081209 081209We need to add an "A" to the end of every "SDT" string only by the first case(witout "POP")
The script you send me add an "A" in both cases!
Any suggestion?

You said the \/\/ODETT\/\/INVGM marks the change of the invoice. It shouldn't matter just because the file is reversed.
Is there a string beginning with FTX for each invoice also also? I don't see it in the data set you provided.
I was going to rewrite the comparisons for you, but I won't until I have a better idea of the data you are using.

The Invoice beginn like this:
//ODETT//INVGM
UNB 090106
MID 00000047 090106
SDT012837259
BDT01315000737
PAI EUR 090106
RAD POP081204 081204
CSG0172261
CSG02
ARD0113268023
PRI 000000001NTP000000000000000
ARD0113268176
....
....
and end like that:
....
VAT 0019.000000000000003.8
TLR -00000000019.98-00000000023.76
FTX Debit note retro active price adjustmentsand then beginnt the next Invoice segment:
//ODETT//INVGM
UNB 090106
MID 00000043 090106
SDT012837259
BDT01315000737
PAI EUR 090106
RAD POP080722 080722
CSG0172261
....
.....
TLR -00000022234.24-00000026458.79
FTX Debit note retro active price adjustments.And so on. Like you see the beginn of the Invoice Segement is"//ODETT//INVGM"
and the end is " FTX Debit ...."I thaught wenn we reverse the file the "FTX" string will mark the beginn of the invoice segement and in this case I remplace in your script the string "//ODETT//INVGM" with "FTX".
But any way the script does not work properly in both cases; it add an "A" to the end of EVERY "SDT" string aside from the "RAD" segment number type(with POP or without).Sorry for the confusion Nails.
Is ther any possibilty to send the invoice file?

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

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