Read few last lines of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read few last lines of a file
# 1  
Old 09-18-2012
Read few last lines of a file

Hi,

I have a txt file in below format -
Code:
START
1
2
3
4
END
START
5
6
7
8
END
START
9
10
END

My requirement is to go at the last "START" (highlighted in BOLD) and read till "END". Can you please suggest me how to achieve it?

Thanks for your time.

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 09-18-2012 at 03:17 AM..
# 2  
Old 09-18-2012
Code:
 awk '{if($0~/START/){i=NR-1;}a[j++]=$0;} END{while(i!=j)print a[i++]}'  input_file

I feel there is a better solution than the above Smilie
This User Gave Thanks to msabhi For This Post:
# 3  
Old 09-18-2012
Code:
awk '/START/{p=1;sect=""}p{sect=sect?sect RS $0:$0}/END/{p=0}END{print sect}' file

This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 09-18-2012
Another way:
Code:
perl -e 'foreach (reverse <>){ if (/START/){print reverse(@a);exit};if (/END/ .. /START/){push (@a,$_) unless /END/}}'  infile

This User Gave Thanks to Klashxx For This Post:
# 5  
Old 09-18-2012
Thanks all for your reply.

The AWK solution seems to be suiting better for my requirement.
"awk '/START/{p=1;sect=""}p{sect=sect?sect RS $0:$0}/END/{p=0}END{print sect}' file"

Here is it possible that I can make use of regular expressions with START and END since there is a numeric value appended. E.g. START3/END3.

On the same question, how can I read the same file Starting START3 and read till end of file without considering the END tag? Which means START3, START4 .. till end will be displayed. Can the existing solution be modified?

Thanks again.
# 6  
Old 09-18-2012
Quote:
Here is it possible that I can make use of regular expressions with START and END since there is a numeric value appended. E.g. START3/END3.
The pattern START will, of course, match START3. What strings do you want to match?
------
Quote:
how can I read the same file Starting START3 and read till end of file without considering the END tag? Which means START3, START4 .. till end will be displayed.
Code:
awk '/START3/{p=1}p' file

This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 09-20-2012
Thanks everyone for your helping hand.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

2. Shell Programming and Scripting

Read in specific lines in a file

I have a text file First title line name1 name2 name3 name4 (etc) other lines other lines other lines I want to read in this text file and parse the name1...name4 to a variable. How do I specificially read in these lines and these lines only? (10 Replies)
Discussion started by: piynik
10 Replies

3. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

4. Shell Programming and Scripting

Why does my script only read two lines of a file and not the third

I'm learning about the read command and wrote this little script to read data from a file: readfile() { while read variable; do echo $variable done } readfile < File.txt I have three lines in File.txt; each a single word. The script only echoes the first two lines and drops the... (9 Replies)
Discussion started by: Straitsfan
9 Replies

5. Shell Programming and Scripting

Read any lines of text from file

Witam wszystkich , Jest to moj pierwszy post i już prośba ale gdybym potrafił zaradzić problemowi to nie zawracałbym nikomu głowy . mianowicie : Mam jakis 'plik' w ktorym są osadzone pojedyncze i zmienne słowa po jednym w lini czyli : test1 tekttw resst .... itd. Moje... (6 Replies)
Discussion started by: versace
6 Replies

6. Shell Programming and Scripting

read some lines from file!!!

any idea please!!! I want to pick up all lines of "state" and "desc" from x files: ... # state blah blah blah blah ... .. # desc blah blah blah .... Thx Andy (7 Replies)
Discussion started by: andy2000
7 Replies

7. UNIX for Dummies Questions & Answers

Read lines from file

i have a problem on my bourne shell script. I want to read line by line and then stop when the line does not have letters or is an empty string. But i encounter an error at "while ". The error nessage is "test.sh: test: unknown operator line". Can anyone help me on this thanks :) (2 Replies)
Discussion started by: sagolo
2 Replies

8. UNIX for Dummies Questions & Answers

how to read lines one by one from a file

I have one file in which some commands have written line line i have to read lines from this file(file name passed as avariable) and then i have to execute these commands.. how can i do it? (5 Replies)
Discussion started by: bihani4u
5 Replies

9. Shell Programming and Scripting

Read the lines from the file in batch

Hi, I want to read lines in a loop. eg. In a file with 100 lines..first I want to read first 1 to 10 lines and redirect it to other file, then next 10 lines ( 11 to 20 ) and redirect it to the file ... .till end of file. I am not sure how to achieve this.Need help. (2 Replies)
Discussion started by: amitraorane
2 Replies

10. Programming

How to read specific lines in a bulk file using C file Programming

Please Help me I have a Bulk file containing Hex data I want to read specific lines from that bulk file by ID number. example ID DATE Time data 14 2005/09/28 07:40:08.546 0 5 078B1C 01916C 0FE59C 004B54 0A9670 0D04ED 05B6B4 0E2223... (10 Replies)
Discussion started by: rajan_ka1
10 Replies
Login or Register to Ask a Question