• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to fetch rows based on line numbers or based on the beginning of a word

 
rahulJ james
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rahulJ james
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me in this
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic