I have a file which will have rows like shown below,
ST*820*316054716
RMR*IV*11333331009*PO*40.31
REF*IV*22234441009*xsss471-2762
DTM*003*091016
ENT*000006
RMR*IV*2222234444*PO*239.91
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*
091016
RMR*IV*2223344441009*PO*40.31
REF*IV*111333331009*kjhjjkhkhk471-2762 </SPAN>
SE*000030*316054716
The user will be entering an account number 2222234444 and I have a script which will get the RMR record which holds that account number and which will be displayed to the user for editing, something like,
RMR*IV*2222234444*PO*239.91
Now the user wants the rows under RMR as well, display the RMR and the following segments until the next RMR is located. something like,
RMR*IV*2222234444*PO*239.91
REF*IV*1234445451009*LJhjlkhkj471-2762
DTM*003*091016
I thought of doing it by finding the line numbers of RMR and the next RMR and copying those records. Please advise me with an optimized solution
Right now I am finding the RMR row by
grep -n "^RMR.*$acct.*" file > output . Is there a simple way where I can get the other rows along with this row until I meet the next RMR
clx
January 6, 2010, 9:31am
2
will this work??
awk ' /2222234444/ && /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' file
I could get the result with a simple grep command grep -n "^RMR.*1222334444.*" file.
I need to fetch this RMR and the successive rows until the next RMR
So I shud have something like,
RMR*.......
DTM*......
clx
January 7, 2010, 5:15am
4
did you check the o/p of the above command?
Yes, it gave me only the RMR row and not the expected result
clx
January 7, 2010, 5:19am
6
can you show us the o/p?
this is what I am getting:
bash$ awk ' /2222234444/ && /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' file
RMR*IV*2222234444*PO*239.91 </SPAN>
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*091016 </SPAN>
bash$
I am sorry, I did a mistake and overlooked. You are right
I got the result as expected. Now rather than giving the pattern as hardocded, can I give something like this
acct=5039775144131
awk ' /$acct/ && /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' $HOME/Input > $HOME/out11
I tried and it gives me an error
xoops
January 7, 2010, 5:45am
8
If you are looking to put a variable there.
awk ' /'$acct'/ && /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' $HOME/Input > $HOME/out11
I tried something like this and it worked,
acct=5039775144131
awk ' /'"$acct"'/&& /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' $HOME/Input > $HOME/out11
Thanks for your help
It doesnt work when there is only one RMR in loop. It works for all the scenarios.
When I have something like,
RMR2........
DTM 3.......
It works only If I have something like below,
RMR4.......
DTM 9......
RMR*7.......
Please advise
---------- Post updated 01-13-10 at 12:37 AM ---------- Previous update was 01-12-10 at 12:43 PM ----------
It doesnt work when there is only one RMR in a loop.
it works only for something like,
RMR*.......
DTM*.......
RMR*.......
and not if it has only one RMR,
RMR*.....
DTM*.....
Can you advise
clx
January 13, 2010, 1:01am
11
up to which record you want the o/p in that case?
RMR*IV*2222234444*PO*239.91 </SPAN>
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*091016 </SPAN>
AAA*IV*2223344441009*PO*40.31 </SPAN>
REF*IV*111333331009*kjhjjkhkhk471-2762 </SPAN>
SE*000030*316054716 </SPAN>
what will be the required o/p from the above?
If this is the data,
RMR*IV*2222234444*PO*239.91 </SPAN>
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*091016 </SPAN>
RMR*IV*2223344441009*PO*40.31 </SPAN>
REF*IV*111333331009*kjhjjkhkhk471-2762 </SPAN>
SE*000030*316054716 </SPAN>
I need the output till DTM, that is the last segment for an RMR loop.
Output should be
RMR*IV*2222234444*PO*239.91 </SPAN>
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*091016 </SPAN>
clx
January 13, 2010, 7:52am
13
/home->awk ' /2222234444/ && /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' a
RMR*IV*2222234444*PO*239.91 </SPAN>
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*091016 </SPAN>
/home->cat a
RMR*IV*2222234444*PO*239.91 </SPAN>
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*091016 </SPAN>
RMR*IV*2223344441009*PO*40.31 </SPAN>
REF*IV*111333331009*kjhjjkhkhk471-2762 </SPAN>
SE*000030*316054716 </SPAN>
/home->
this is working.
i did not understood what you are trying to say from "the last segment.
I intentionally changed the RMR to AAA to show what I understood.
When I have RMR twice in my input, the awk script works good as you had mentioned.
When there is only one RMR in my Input, I get all the records where I should get only the relevant records.
For example, if the input is as mentioned below,
RMR*IV*2222234444*PO*239.91
REF*IV*1234445451009*LJhjlkhkj471-2762
DTM*003*091016
SE*000030*316054716
I should get the output as
RMR*IV*2222234444*PO*239.91
REF*IV*1234445451009*LJhjlkhkj471-2762
DTM*003*091016
and without the last SE record which doesnt belong to RMR loop. Hope this explains you
alister
January 13, 2010, 9:08am
15
Solution:
awk -F\* '/^RMR/ && $3==a,/^DTM/' a="$a" inputfile
Test Run:
$ a="2222234444"
$ cat data
ST*820*316054716
RMR*IV*11333331009*PO*40.31
REF*IV*22234441009*xsss471-2762
DTM*003*091016
ENT*000006
RMR*IV*2222234444*PO*239.91
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*
091016
RMR*IV*2223344441009*PO*40.31
REF*IV*111333331009*kjhjjkhkhk471-2762 </SPAN>
SE*000030*316054716
$ awk -F\* '/^RMR/ && $3==a,/^DTM/' a="$a" data
RMR*IV*2222234444*PO*239.91
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*
$ cat data2
RMR*IV*2222234444*PO*239.91
REF*IV*1234445451009*LJhjlkhkj471-2762
DTM*003*091016
SE*000030*316054716
$ awk -F\* '/^RMR/ && $3==a,/^DTM/' a="$a" data2
RMR*IV*2222234444*PO*239.91
REF*IV*1234445451009*LJhjlkhkj471-2762
DTM*003*091016
This works for the scenarios you have mentioned, it will not work when it has
Scneario1
RMR*.....
REF*....
SE*.....
Scneario2
RMR*....
SE*.....
Scneario3
RMR*
DTM*....
SE*....
Can you help me..
rdcwayx
January 15, 2010, 1:15am
17
acct=5039775144131
awk 'BEGIN{RS="";FS="\n"} /RMR/&&/'"$acct"'/' $HOME/Input > $HOME/out11
I have the solution for this issue, I need the line numbers of each row when I fetch. .
awk ' /'"123344444"'/&& /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' /tmp/input > /tmp/output
With this my output will look like
RMR*IV*123344444**123.45*123.45
NTE*ABC*DEF#J-123-444-3333877M
DTM*003*091101
Where in I need the output like
11:RMR*IV*123344444**123.45*123.45
12:NTE*ABC*DEF#J-123-444-3333877M
13:DTM*003*091101
I know I can use awk '{print FNR "\t" $0}' files to get the line number. But how Do i merge with my result above
Can anyone advise
clx
February 2, 2010, 4:27am
19
since you have only one input file, you can use NR too.
repalce print with
print NR":"$0
or
print FNR":"$0