Find the position of lines matching string

I have a file with the below format,

GS*8*****
ST*1********
A*
B*
E*
RMR*123455(This is the unique number to locate this row)
F*
SE*1***
GE**
GS*9*****
ST*2
H*
J*
RMR*567889(This is the unique number to locate this row)
L*
SE*
GE*****
 

With the unique number above, I want to fetch the line number of the corresponding GS** and GE***

Can anyone advice

you mean this?

 grep -n -e "GE" -e "GS" txt

or

awk '{if($1 ~ /GS.*/ || $1 ~ /GE.*/) print NR,$1}' txt

txt is the file name of the contents above.

What is corresponding? The GS/GE lines above the RMR or the ones below?

Thanks for your response. I need to get the GS and GE covering the RMR*12345

In my case, by having the unique number (12345) value, I wanted to fetch the GS just above the RMR and the GE below the RMR row

Line numbers, like this?

awk '/^GS/{p=NR}  $0~"^RMR\\*"k{f=1} /^GE/&&f{print p,NR;f=0}' k=123455 infile

(On Solaris, use nawk, or /usr/xpg4/bin/awk instead of awk)

if i understand your problem correctly,it can be solved by this,though this method is redundancess^_^

awk -v num=123455 'BEGIN{flag=0} {if($1 ~ /GS.*/) gs=NR;if($1 ~ "RMR*"+num){ print gs;flag=1} if(flag == 1 && $1 ~ /GE.*/){ print NR;exit}}' txt

@homeboy: How does this: $1 ~ "RMR*"+num work? What does the + signify?

well,i haven't use awk for a long time and i wrote that code without too much thinking,do u mean this kind of usage is seldom to see?:)I saw the man pages just now and it seems that ~ can be used as match a pattern either quoted by // or "".I used the "+" to catenate a string and a value,however,i'm not sure it's a right way,but it did gave a correct result here.:slight_smile:

Remarkable. + is not a concatenation character in awk, but it does something here. But what?
....
I think I figured it out. The + sign forces what follows into numerical context, but also what procedes it. Non-numerical characters turn into 0 (zero) So it becomes equivalent to:

$1 ~ num
2 Likes

:smiley: Thanks,your explanation is compelling.I didn't notice it:wall:

I tried both the snippet and it returns me nothing..

Are you on Solaris? What is you actual command and a sample of your actual input ?

I am using HP-UX.

The command I tried, where bs is the file name and 7203446151111 is teh unique number

awk '/^GS/{p=NR}  $0~"^RMR\\*"k{f=1} /^GE/&&f{print p,NR;f=0}' k=7203446151111 bs

OK, what does:

awk '$0~"^RMR\\*"k' k=7203446151111 bs

give or:

awk '$0~""k' k=7203446151111 bs

Is there an RMR line in the file with that number?

Yeah I do have the value in that as mentioned below. Even your latest code didnt get me any result.

GS*071000013*102562451P*101208*0503*443432*X*003050
ST*820*21321
BPR*C*192388.20*C*ACH*CCD*01*011900254*Z*56065758*56065758  **01*071000013*DA*5529085*101208
TRN*1*011900253240679*56065758
REF*TN*2132132132144*TRACE NUMBER FROM THE ACH PAYMENT
DTM*009*101207
N1*PR*AMER. PAY. SYS.*91*56065758
ENT*1
N1*PE* 19308540
RMR*TN*12322432343**192387.20*192388.20
RMR*TN*012345678777**1*1
SE*000010*342050361
ST*820*342050362
BPR*C*250.00*C*ACH*CTX***DA*232994246B*232994246B**01*071000013*DA*5529085*101208
TRN*1*53000217688618
REF*TN*21321*TRACE NUMBER FROM THE ACH PAYMENT
N1*PR*ALLIANCEONE*91*7203446150923
N1*PE*QWEST
ENT*1
RMR*IV*7203446151111**250.00
SE*000009*213213
GE*2*21321

This deviates a bit from the sample in post#1. Try this instead:

awk '/^GS/{p=NR}$0~"^RMR.*\\*"k{f=1}/^GE/&&f{print p,NR;f=0}' k=7203446151111 infile

Hey Great!! It works. I am able to get the relevant loop numbers.

Thanks again

---------- Post updated at 05:07 AM ---------- Previous update was at 05:06 AM ----------

Can you explain this if you dont mind