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
# 15  
Old 04-10-2013
For Year-to-Date:
Code:
$ sed -n "/2012 Totals Year-to-Date/,/Total interest charged in 2012/ {s/^ *//; p}" data
2012 Totals Year-to-Date

Total fees charged in 2012                              $122.93

Total interest charged in 2012                             $0.00

---------- Post updated at 06:35 PM ---------- Previous update was at 06:17 PM ----------

For Fees:

Code:
$ cat fees.sh
sed -n "/^ *Fees */, /TOTAL INTEREST FOR THIS PERIOD/ p" data > temp
echo "Transaction   Posting                                             Reference   Account" > output
echo "Date       Date           Description                                              Number       Number                 Amount            Total" >> output
tail -n +2 temp | cut -c 10- >> output
cat output

Code:
$ ./fees.sh
Transaction   Posting                                             Reference   Account
Date       Date           Description                                              Number       Number                 Amount            Total
07/07      07/07          LATE FEE FOR PAYMENT DUE 07/07                           3214                                22.00
07/10      07/10          CREDIT PROTECTION PLUS XXX.XXX.XXXX                      500P                                  9.47
                          TOTAL FEES FOR THIS PERIOD                                                                                  $31.47

                          Interest Charged

07/10      07/10          Interest Charged on Purchases                                                                  0.00
07/10      07/10          Interest Charged on Balance Transfers                                                          0.00
07/10      07/10          Interest Charged on Dir Dep&Chk CashAdv                                                        0.00
07/10      07/10          Interest Charged on Bank Cash Advances                                                         0.00
                          TOTAL INTEREST FOR THIS PERIOD                                                                               $0.00

# 16  
Old 04-10-2013
Try this - purely based on the input provided
Code:
awk '/continued/{if(a==2){ha=1;a=1}if(b==2){b=1;hb=1}if(c==2){hc=1;c=1}next}
!x&&/Transaction Posting/{x=1;hdr=$0;getline;hdr=hdr"\n"$0;next}

!a&&/Payments and Other Credits/{a=1;next}
a==1&&/Payments and Other Credits/{a=2;if(!ha){print;print hdr}next}
a==2{print} a==2&&/\$/{a=-1;printf "\n"}

!b&&/Purchases and Adjustments/{b=1;next}
b==1&&/Purchases and Adjustments/{b=2;if(!hb){print;print hdr}next}
b==2{print} b==2&&/\$/{b=-1;printf "\n"}

!c&&/Fees/{c=1;next}
c==1&&/Fees/{c=2;if(!hc){print;print hdr}next}
c==2{print}/TOTAL INTEREST FOR THIS PERIOD/{c=-1;printf "\n"}

/Totals Year-to-Date/,/Total interest charged/{print}' infile

--ahamed

Last edited by ahamed101; 04-10-2013 at 10:32 PM..
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