Grep range of lines to print a line number on match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep range of lines to print a line number on match
# 1  
Old 04-09-2013
Grep range of lines to print a line number on match

Hi Guru's,
I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am looking in.

I actually want it to print the line number of this match (starting with $ symbol) with a range of lines from a file.
an example match could be $x,xxx.xx but not always the same in the length.

Code:
string_contd_nxt_page=`grep -n "continued on next page" sample.txt|cut -d : -f1`
eos_page_1=`echo $string_contd_nxt_page|cut -d " " -f1`
eos_page_2=`echo $string_contd_nxt_page|cut -d " " -f3`
sos_page_2=`echo $string_prch_n_adjsmnts|cut -d " " -f2`
string_Fees=`grep -n Fees$ sample.txt|cut -d : -f1`
Eof_String_Fees=`echo $string_Fees|cut -d " " -f1|awk '{print $1-1}'`

END_Of_Statement=`sed -n "$sos_page_2,$Eof_String_Fees"p sample.txt|grep -Fn '$'|cut -d : -f1`

# 2  
Old 04-09-2013
It would be better if can paste a sample input and expected output.

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 04-09-2013
Sure here is the sample page X of many pages but this is where the match i am interested in....
Code:
**************************** Page 3 ****************************








                                    xxxx xxxx xxxx  xxxx
                                    June 9 - July 10, 2012
                                    Page 3 of 6

       Transactions   continued

       Transaction Posting                                                                Reference    Account
       Date       Date           Description                                              Number       Number                 Amount            Total

                                 Purchases and Adjustments
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   7907         2788                     1.25
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   6291         2788                   21.28
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   0917         2788                     8.43
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   5704         2788                     1.25
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   4753         2788                   42.83
                 
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   8560         2788                  120.82
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   5185         2788                   26.42
                 ......................................
                 ......................................
                 ......................................
                 ......................................
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   5393         2788                   14.13
       MM/DD      MM/DD          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                   3611         2788                   14.92
                                 

                                                                                  THIS IS THE MATCH I NEED THE LINE NUMBER FOR ============>  $1,350.74
**************************** Page 4 ****************************




       Transactions   continued

       Transaction Posting                                                                Reference    Account

# 4  
Old 04-09-2013
You need to give more details on the expected output.
So all you want is the line number for $1,350.74?

--ahamed
# 5  
Old 04-09-2013
Quote:
Originally Posted by ahamed101
You need to give more details on the expected output.
So all you want is the line number for $1,350.74?

--ahamed

yes, all i want is the line number for $1,350.74, but within a range of line numbers specified.
# 6  
Old 04-09-2013
Try this
Code:
start=100
end=120
awk '/'$start'/,/'$end'/{if(match($0,/\$/)){print NR}}' infile

Code:
awk 'NR>=start && NR<=end{if(match($0,/\$/)){print NR;exit}}' start=100 end=120 infile

Code:
sed -n '/'$start'/,/'$end'/{/\$/=}' infile

where start and end specified the range of line numbers.
Using NR or FNR for print depends on if you have one file or multiple files.

--ahamed

Last edited by ahamed101; 04-09-2013 at 03:52 PM..
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 04-09-2013
For the data you have supplied, the following does what you want:
Code:
awk -F'$' 'NF > 1{print NR}' sample.txt

If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question