Position of the string in a complex file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Position of the string in a complex file
# 1  
Old 04-19-2011
Position of the string in a complex file

I had a similar problem few days back and got this fixed with the below command when I have a file with this format

HTML Code:
GS*12345*****
ST*1********
A*
B*
E*
RMR*123455(This is the unique number to locate this row)
F*
SE*1***
GE*12345*
GS*878787*****
ST*2
H*
J*
RMR*567889(This is the unique number to locate this row)
L*
SE*
GE*878787****
And with this code, I was able to find the location of account '56789'
HTML Code:
 awk '/^GS/{p=NR}$0~"^RMR.*\\*"k{f=1}/^GE/&&f{printf("%d %d\n",p,NR);f=0}' k="567889" filename
However, when I have a file with duplicate account number in different GS-GE loops, I get both the occurrence with the above command. How do I fetch the exact GS-GE loop when there is a duplicate account number in 2 different GS-GE. I will also have the '12345' in GS row as the search string. So basically, I will have '12345' from GS and '56789' from RMR as my search strings.

HTML Code:
GS*12345*****
ST*1********
A*
B*
E*
RMR*123455(This is the unique number to locate this row)
F*
SE*1***
GE*12345*
GS*676767*****
ST*2
H*
J*
RMR*567889(This is the unique number to locate this row)
L*
SE*
GE*676767****
GS*787878*****
ST*3
A*
B*
RMR*567889(This is the unique number to locate this row)
L*
SE*
GE*787878****
# 2  
Old 04-19-2011
Try this

Code:
awk 'BEGIN{RS="GS"} /RMR\*12345/ {print}' file | grep <whatever value you want>

awk 'BEGIN{RS="GS"} /RMR\*12345/ {print}' file | grep RMR
awk 'BEGIN{RS="GS"} /RMR\*12345/ {print}' file | grep GE

making RS = GS, will give you one GS-GE loop within one print.

regards,
Ahamed
# 3  
Old 04-19-2011
Thanks for your response.

It gives me the below error when I execute your command.

HTML Code:
awk: Input line LADYS
RMR*IV*6025690 cannot be longer than 3,000 bytes.
 The input line number is 98. The file is rps_bank1_recs_base.
 The source line number is 1.
# 4  
Old 04-19-2011
Can you paste the exact command you executed and the output?

regards,
Ahamed
# 5  
Old 04-19-2011
This is what I executed

HTML Code:
awk 'BEGIN{RS="GS"} /RMR\*6121258075/ {print}' file | grep "6121258075"
---------- Post updated at 02:19 AM ---------- Previous update was at 02:16 AM ----------

Can you tell me how to pass the variable to the GS as I have done it for RMR with the below command. I wanted to pass the unique number in GS row along with this.. How do i do that?

HTML Code:
awk '/^GS/{p=NR}$0~"^RMR.*\\*"k{f=1}/^GE/&&f{printf("%d %d\n",p,NR);f=0}' k="60593347"
# 6  
Old 04-19-2011
Which OS and Shell are you using? The same command is working fine for me. The "file" is the actual file where the contents are present.

Code:
/user/ahamed $ awk 'BEGIN{RS="GS"} /RMR\*12345/ {print}' file | grep RMR
RMR*123455(This is the unique number to locate this row)

However, the above logic will fail if you have same RMR values.

Lets go to the beginning. What is your requirement?
Let say, this is your file
Code:
GS*12345*****
ST*1********
A*
B*
E*
RMR*123455(This is the unique number to locate this row)
F*
SE*1***
GE*12345*

And I understand you will have many GS-GE groups. You will have 12345(GS) and 123455(RMR)
as the search string. So what is it that you want from this GS GE loop?

regards,
Ahamed
# 7  
Old 04-19-2011
Of course, I replaced the 'file' with my actual file name.

What I wanted is, that If the same account number is present in different GS-GE loops, then I should pick the exact loop which matches the RMR account number..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I replace a string in file that is in a certain position with spaces?

I am trying to replace the string in position 26 through 35 of the data file with 10 spaces and I want the remaining file to stay as is, the record length is over 900 characters? I am trying to use the AWK and substr but I am not getting it formatted correctly. Before... (6 Replies)
Discussion started by: fnwine1500
6 Replies

2. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

3. Shell Programming and Scripting

Search string at a particular position in a file

Hi, i have a text file as : abc 0 1 Pass hjk 1 1 Pass bhk 0 0 Fail jjh 8 2 Pass nkji 0 1 Pass Now I want to check that if 1st column is jjh , then , store the value of 3rd string of that line in a variable. Hence, 2... (8 Replies)
Discussion started by: Anamika08
8 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

7. Shell Programming and Scripting

To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field. For example i have a file called Temp.txt having content Temp.txt ----------------- 100,234,M1234 400,234,K1734 300,345,T3456 ---------------- So the modified file output should... (4 Replies)
Discussion started by: rpadhi
4 Replies

8. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

9. UNIX for Dummies Questions & Answers

Add a string in the specified position of the file

I have a file and it contains some text and the length is 145. I need to add a string which is of length 8 at the 120th position. Is it possible to add???? Need help on this....:( (4 Replies)
Discussion started by: sivakumar.rj
4 Replies

10. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies
Login or Register to Ask a Question