To find a character immediately following a specified String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To find a character immediately following a specified String
# 1  
Old 07-23-2008
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.
# 2  
Old 07-23-2008
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;
}
# 3  
Old 07-23-2008
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.
# 4  
Old 07-23-2008
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'.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find directory immediately after the pattern dir name

Hi, From below directories path I need the directory which comes immediately after the "DataPath" /var/errors/LogDefaultPath/DataPath/Data01/Data02 (Directory name "Data01" is the result from this path) /var/errors/LogDefaultPath/DataPath/Log01/Log0202 (Directory name "Log01" is the... (4 Replies)
Discussion started by: Yuvaan12
4 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

3. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

4. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

5. Shell Programming and Scripting

Find string in a file and append character

Hi Experts, Is there a way to find a string in a file then append a character to that string then save the file or save to another file. Here is an example. >cat test.txt NULL NULL NULL 9,800.00 NULL 1,234,567.01 I want to find all NON NULL String and add a dollar sign to those... (9 Replies)
Discussion started by: brichigo
9 Replies

6. Shell Programming and Scripting

read a string from its end to its start and stop when you find a specific character

How can I do this? Actually I have a file which contains a path e.g. /home/john/Music/hello.mp3 and I want to take only the filename (hello.mp3) So, I need to read the file from its end to its start till the character "/" Is this possible? Thanks, I am sure you'll not disappoint me here! Oh,... (9 Replies)
Discussion started by: hakermania
9 Replies

7. Shell Programming and Scripting

Find the middle character from a string using sed

Input: qwertmyuiop It should print "m". What's the command to do this with sed? (6 Replies)
Discussion started by: cola
6 Replies

8. Shell Programming and Scripting

Find index of last occurence of a character within a string

I need to find the index of last '|' (highlighted in bold) in awk : |ifOraDatabase.Lastservererr<>0then|iferr_flag<>0then|end if Please suggest a way... Thanks (5 Replies)
Discussion started by: joyan321
5 Replies

9. Shell Programming and Scripting

To find a character immediately following a word

Hello, I have data in one file like - UNB+UNOA:1+006415160:1+WF0000010188:ZZ+080701:0600+2++DELFOR++++T'UNH+2+DELFOR:D:97A:UN................ Now, I want to find what is the character immediately following UNB(here +) UNOA (here :) or the character immedialtely preceding UNH (here ') &... (2 Replies)
Discussion started by: The Observer
2 Replies

10. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies
Login or Register to Ask a Question