File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes how to fetch rows based on line numbers or based on the beginning of a word Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "how to fetch rows based on line numbers or based on the beginning of a word" Watch "how to fetch rows based on line numbers or based on the beginning of a word" New topic
Author

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
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: 41489
    
  53
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.


Ping & DNS - my free Android networking tools app
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
Right now I am finding the RMR row by . Is there a simple way where I can get the other rows along with this row until I meet the next RMR
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
Can anyone help me in this
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
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.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11422
    
  85

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.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.



http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to fetch rows based on line numbers or based on the beginning of a word