To find a character immediately following a specified String

Hello,

I have a UNIX file in which data is like this --

ISA*00* *00* *01006415160 *01137361242 *080125*2134*U*00401*000000693*0*P*~<GS*IN*04373CD*097248199*20080125*2134*0000693*X*004010<IT1**4*EA*6.7**VP*3682710*PD*GSK,MAN<IT1**1*EA*6.7**VP*3682940*PD*GSK,MAN.............................................

This file is an 'atext'ed file ('atext'ed file means all the data in this file is streamlined in one single line).I want to find the character immediately preceding the first occurence of 'GS' in this one line. For that, I used following command -

/usr/xpg4/bin/sed 's/.*\(.\)GS.*/\1/' <filename>

As per expectation, this should give me the answer as - '<' But, it gave me the answer as '*' . This means, the 'sed' command is giving me the character preceding the last occurrence of 'GS'. If we don't use 'g' option in 'sed' command, then we should get the 'sed' being applied to only first occurrence. I am not understandig why this is not working.

Please help.

I know all too well what that data is. That looks like an 837 transaction file.
I have a little C program i use to split it into its segments. It uses the standard input and output

unx12 < inputfile > outputfile

It goes like this (I wrote it so feel free to cut and paste it)

/* unx12.c : breaks ANSI 837 file into segments */
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int x=0;
int u=0;
char c;

while (scanf("%c",&c)!=EOF)
{
x++;
if (c=='~')
{
u=0;
printf("%c",c);
printf("\n");
}
else
if (c == 0x0d)
{
/* do nothing /
}
else
if (c == 0x0a)
{
/
do nothing */
}
else
{
u++;
printf("%c",c);
}
}

return 0;
}

Sorry slant-40, but I want to do all this in a Shell script & not in a C Program.
Also, I am trying to find the character preceding 'GS' to find the segment delimiter in that file as in our system there might be different segment delimiters for different files. I will store that delimiter in a variable & then process the file.

Do u have some option in 'sed' command itself so that it will take only the first occurrence of 'GS' & return the result.

By the way, this is 810 transaction set file.

Try looking for the string 'GSIN' or 'GS*IN' since that should always be the first occurrence of the 'GS' string in any 810. We have similar files (healthcare transactions) that we work with. I will ask around since I'm not too familiar with 'sed'.