Sponsored Content
Top Forums Shell Programming and Scripting Grep range of lines to print a line number on match Post 302792493 by Don Cragun on Wednesday 10th of April 2013 01:24:32 PM
Old 04-10-2013
Quote:
Originally Posted by Kevin Tivoli
All,

Thanks for all your replies.....

Sorry for not being descriptive initially, let me try this time....

Basically, I am trying to print this page (besides many pages i am able to) with all the transactions listed till the line total amount in "$" amount. A sample data is posted above.
No, a sample is not posted above. All that you have shown us is part of a page 3 and part of a page 4 of a sample of a single transaction. You have not shown us anything that contains "_Start", "_End", "Purchases and Adjustments", or any other string you say you're looking for (except for the "$").
Quote:
For this i am defining the start of this specific page based on the match as follows....
Code:
Page(n)_Start = Line number of last occurrence of the match "Purchases and Adjustments" in the whole input file.

Code:
Page(n)_End = Line number of Fixed string match "Fees" - 1 in the input file

(This should ideally print lines till the total "$" amount I am interested in.)

I am able to this fine but sometimes (different input file) there is some junk data between the "$" amount and the "Page(n)_End" which I don’t want to be displayed and hence I am trying to see if there is way to print the line number of this match "$" amount between "Page(n)_Start" and "Page(n)_End" and either make that line number the Actual/New "Page(n)_End" or print only till that "$" amount line.

Details of the Match "$" amount:-
It starts with a "$" character and rest are always numbers but it could have any numbers of characters.
This is just an example: -
Code:
$ x,xxx.xx

The reason I am trying to search for this match between specific lines is because such matches are in multiples in the input file.


Please let me know if need more details....

Thanks again
It appears that you are asking us to help you produce an extremely inefficient way to separate statements into separate files or to discard unwanted text between statements and just print the statements from a file (or from multiple files) containing multiple statements. You say that only numbers follow the "$", but that is not true ("," and "." are not numbers). And why do we care? The only $ in the entire sample you have shown us is $ you want to find. So, my script gave you the line numbers of all lines containing a $. If there are multiple statements (or transactions) in your input file and you have set $start in your shell to be the 1st line of a transaction and you just want the line number of the 1st line after that that contains a $, try:
Code:
awk -F'$' -v low="$start" 'NF<2 || NR<low{next}{print NR; exit}' sample.txt

If you'd like us to help you create a script that will throw away everything between "transactions" or split "transactions" into separate files efficiently, then give us a real example and an accurate description of how to identfy the 1st and last lines of a "transaction", and then describe what you want to be done with the "transactions" found.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies

2. UNIX for Dummies Questions & Answers

grep N lines after match and then print them on 1 line each

Hello I have a silly question. I need to grep a match in text file and then print 5 lines after it. grep -A 5 .... do it. OK The next thing I can not handle is I need each output to be on 1 line match line2 line3 line4 line5 match line2 line3 line4 line5 etc.. I will really... (10 Replies)
Discussion started by: alekkz
10 Replies

3. Shell Programming and Scripting

print lines between line number

Hi, Anyone help me to print the lines from the flat file between 879th line number and 1424th line number. The 879 and 1424 should be passed as input to the shell script(It should be dynamic). Can any one give me using sed or awk? I tried using read, and print the lines..Its taking too... (3 Replies)
Discussion started by: senthil_is
3 Replies

4. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

5. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

6. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

7. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

8. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

9. Shell Programming and Scripting

How to print N number of lines before and after the grep?

Hi , My record file , need to print up to above (DATA array)(there may be n no lines ) , grep "myvalue" row now .....suggest me some options --- DATA Array--- record type xxxxx sequence type yyyyy 2 3---> data1 /dev/ --- DEVICE --- MAXIMUM_People= data_blocks= MY_value=2 xyz abc ... (0 Replies)
Discussion started by: Huvan
0 Replies

10. UNIX for Beginners Questions & Answers

Advise on how to print range of lines above and below a number?

Hi, I have attached an output file which is some kind of database file mapping. It is basically like an allocation mapping of a tablespace and its datafile/s. The output is generated by the SQL script that I found from 401 Authorization Required Excerpts of the file are as below: ... (2 Replies)
Discussion started by: newbie_01
2 Replies
All times are GMT -4. The time now is 10:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy