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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to fetch rows based on line numbers or based on the beginning of a word?
# 8  
Old 01-07-2010
If you are looking to put a variable there.


Code:
 
awk ' /'$acct'/ && /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' $HOME/Input > $HOME/out11

# 9  
Old 01-07-2010
I tried something like this and it worked,

Code:
acct=5039775144131
awk ' /'"$acct"'/&& /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }' $HOME/Input > $HOME/out11

Thanks for your help

Last edited by Scott; 01-13-2010 at 06:28 AM.. Reason: Added
# 10  
Old 01-13-2010
It doesnt work when there is only one RMR in loop. It works for all the scenarios.

When I have something like,

RMR*2........
DTM*3.......

It works only If I have something like below,

RMR*4.......
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
# 11  
Old 01-13-2010
up to which record you want the o/p in that case?

Code:
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?
# 12  
Old 01-13-2010
If this is the data,
Code:
 
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
Code:
RMR*IV*2222234444*PO*239.91 </SPAN>
REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN>
DTM*003*091016 </SPAN>


Last edited by Scott; 01-13-2010 at 06:24 AM.. Reason: Please use code tags
# 13  
Old 01-13-2010
Code:
/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.
# 14  
Old 01-13-2010
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,

Code:
RMR*IV*2222234444*PO*239.91 
REF*IV*1234445451009*LJhjlkhkj471-2762 
DTM*003*091016 
SE*000030*316054716

I should get the output as

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] awk solution to add sequential numbers based on a word

Hi experts, I've been struggling to format a large genetic dataset. It's complicated to explain so I'll simply post example input/output $cat input.txt ID GENE pos start end blah1 coolgene 1 3 5 blah2 coolgene 1 4 6 blah3 coolgene 1 4 ... (4 Replies)
Discussion started by: torchij
4 Replies

2. Shell Programming and Scripting

Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format: I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one. Any help would be greatly appreciated. (10 Replies)
Discussion started by: nwalsh88
10 Replies

3. UNIX for Dummies Questions & Answers

Assigning rank to rows of numbers based on the last column

I have a tab delimited text file that looks like the following: ERBB3 0.00097 IL31RA 0.000972 SETD5 0.000972 MCART1 0.000973 CENPJ 0.000973 FNDC6 0.000974 I want to assign a number to each row based on the value in the last column (in the order of increasing value so that the first row... (3 Replies)
Discussion started by: evelibertine
3 Replies

4. Shell Programming and Scripting

Splitting file based on line numbers

Hello friends, Is there any way to split file from n to n+6 into 1 file and (n+7) to (n+16) into other file etc. f.e I have source pipe delimated file with 20 lines and i need to split 1-6 in file1 and 7-16 in file2 and 17-20 in file 3 I need to split into fixed number of file like 4 files... (2 Replies)
Discussion started by: Rizzu155
2 Replies

5. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. UNIX for Advanced & Expert Users

cut words based on the word count of a line

I would like to cut words based on the word count of a line. This over here inspired me with some ideas but I wasn't able to get what I needed. https://www.unix.com/shell-programming-scripting/105841-count-words-each-line-file-using-xargs.html If the line has 6 words I would like to use this.... (8 Replies)
Discussion started by: cokedude
8 Replies

7. Shell Programming and Scripting

awk fetch numbers after the word

Hi, I would want to fetch all the numbers after a word the number of characters could very. how can I do that? below is the example of the data and the expected output sample data 03 xxxx occurs 1090 times. 04 aslkja occurs 10 times. I would want to fetch 10 & 1090 separately. (13 Replies)
Discussion started by: ahmedwaseem2000
13 Replies

8. Shell Programming and Scripting

Insert rows based on line numbers

Can I insert rows based on line numbers. Say If I need to insert 1 or more rows in a file from line number 10. Can I do that in UNIX I have a file something like A B C D E F After row C, I wanted to add 2 records as X and Y. I have the line number after C as my reference. Can I... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

9. Shell Programming and Scripting

Delete rows based on line numbers in a file

I have to find the number of rows in a file and delete those many rows in another file. For example, if I have 3 rows in a file A, i have to delete first 3 rows in anothe file B, I have the code, it works as standalone, when I merge this with m application (c with unix), it doesnt work. ... (2 Replies)
Discussion started by: Muthuraj K
2 Replies
Login or Register to Ask a Question