The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn: How to loop through a string character by character shew01 Shell Programming and Scripting 10 9 Hours Ago 04:58 AM
To find a character immediately following a word The Observer Shell Programming and Scripting 2 07-11-2008 04:21 AM
cut from a particular character to the end of the string grajesh_955 Shell Programming and Scripting 2 05-25-2008 03:03 AM
Find and replace character in a string callimaco0082 UNIX for Dummies Questions & Answers 7 04-10-2008 07:47 AM
converting character string to hex string axes High Level Programming 5 09-20-2006 10:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-23-2008
Registered User
 

Join Date: May 2008
Posts: 18
To find a character immediately following a specified String

Hello,

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

ISA*00* *00* *01*006415160 *01*137361242 *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.
Reply With Quote
Forum Sponsor
  #2  
Old 07-23-2008
Registered User
 

Join Date: Apr 2007
Location: West Babylon NY 11704
Posts: 5
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;
}
Reply With Quote
  #3  
Old 07-23-2008
Registered User
 

Join Date: May 2008
Posts: 18
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.
Reply With Quote
  #4  
Old 07-23-2008
Registered User
 

Join Date: Apr 2007
Location: West Babylon NY 11704
Posts: 5
Try looking for the string 'GS*IN' 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'.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0