This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
how to fetch rows based on line numbers or based on the beginning of a word
rahulJ james
Ranch Hand
Joined: Oct 03, 2008
Posts: 123
posted
0
I have a file which will have rows like shown below,
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
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Assuming that there are line endings between the lines (and thus you can use a Reader to handle the lines), you could just read the file line by line until a line contains the RMR you're looking for, and then read the next line as well.
It would be helpful if you acknowledged responses that you get so that we know whether they are helpful or not.
For example, I can suggest that a command such as:
will probably do what you want.
However without any acknowledgment of previous posts, I do not know if you are interested in awk solutions, or whether you require perl solutions or php solutions or ???
Likewise, I threw that command line together in around 30 seconds. I could probably make it a lot easier to read and understand, but there is no point for me to do that at present since I have no idea whether you are interested in learning from this.
Ulf Dittmer wrote:It would have been useful to mention that you didn't want to use Java to accomplish this, but were looking for Unix command line tools.
Agrep is a grep variant that can retrieve more than a single line for each hit.
would grep 3 lines after the match, but it seems as if it is not allways the same number of lines till the next RMR or whatever is searched for.