How to Strip lines off Streamed EDI Output

Attached is a streamed EDI ANSI X12 output where the segment terminator/delimiter is a tilde ~ character.

Is it possible to do the following pseudo-code in a unix script (using either sed, awk and/or grep)?
Open file StreamedOutput.txt
Search for ISA and delete the data up to the tilde ~ char
Search for GS and delete the data up to the tilde ~ char
And write the �middle' bit of data to the same filename.

In other words I want to strip off the first (ISA) and last segment (GS) of this file all up to the tilde and leave me the bit of data in the middle.

I tried to use the following script but not having much joy;
x=`grep -n "ISA" StreamedOutput | cut -d "~" -f1`
y=`grep -n "GS" StreamedOutput | cut -d "~" -f1`
a=`echo ${x} + 1 | bc`
b=`echo ${y} -1 | bc`
echo $x $y $a $b
sed -ne '${a},${b}p' StreamedOutput

If I unstreamed the input data it looks like;
ISA*HDR*START*US0050000013~
BA1*Y**A*US*0050000013***TN
Federal Express~
YNQ*RZ
Y~
YNQ*QQ
Y~
DTM*274
081015~
N1*EXSmith & Nephew, Inc.*24510123924RT~
N2*19013995343~
N3
EAST HOLMES RD~
N4*Memphis*TN*38118US~
N1**CAREY STEVE~
N3
1720 EAST BLACKHAWK DR~
N4*PHOENIX*AZ*85024US~
N1*FW
Federal Express24~
N3
123 Main~
N4*MEMPHIS*TN**US~
L13*A*9021.10.0050***10*20****K*0Bone plates, screws & nails & other inte*OSD~
L13*A*9021.31.0000***10*1000****K*0Artificial joints & parts & accessories*OSF~
L13*A*9021.31.0000***10*1000****K*0Artificial joints & parts & accessories*OSD~
GS*TRL*END*US*0050000013~

And the result I want is;
BA1*Y**A*US*0050000013***TNFederal Express~
YNQ*RZ
Y~
YNQ*QQ
Y~
DTM*274
081015~
N1*EX
Smith & Nephew, Inc.*24510123924RT~
N2*1
9013995343~
N3EAST HOLMES RD~
N4*Memphis*TN*38118
US~
N1CAREY STEVE~
N31720 EAST BLACKHAWK DR~
N4*PHOENIX*AZ*85024
US~
N1*FWFederal Express24~
N3*123 Main~
N4*MEMPHIS*TN
US~
L13*A*9021.10.0050***10*20****K*0Bone plates, screws & nails & other inte*OSD~
L13*A*9021.31.0000***10*1000****K*0Artificial joints & parts & accessories*OSF~
L13*A*9021.31.0000***10*1000****K*0Artificial joints & parts & accessories*OSD~

Try it first without the colored fragment of the code

awk '/^ISA/||/^GS/{next}{print}' file > newfile && mv newfile > file

Would two grep's to exclude work?

grep -v "^ISA" file75 | grep -v "^GS"

Or one grep

grep -v '^[ISA|GS]' file

Thanks for the replies, however how will your code change if the data was streamed (i.e. one long line of data in this case) so the delimiter separating the segments is the tilde ~ char.

Try awk:

awk -F'~' '{for(i=1;i<NF;i++){if($i !~ /^[ISA|GS]/)printf $i FS}}'

Hi Danmero, would it be too much to ask for a breakdown of your code and it's intentions/meaning as I tried to code using a combination of grep and sed? Thanks!

stdout | \                #pipe the output to awk, one line only
awk -F'~' '{              #set the field separator to ~
for(i=1;i<NF;i++)         #for each field except the last one(you don't have one anyway)
  { 
   if($i !~ /^[ISA|GS]/)  #if field Don't start with ISA or GS
      printf $i FS        #print(f) in the same line the field plus the field separator(~)
   }
}'

just copy/paste and try.