Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to return certain line of a file 2

Status
Not open for further replies.

zhenning

Technical User
Sep 22, 2005
50
CA
For example, if the input of the script is 3, I need to get the 3rd line from the bottom of the file as the output. How can I make it?

Thanks a lot!

Zhenning
 
Hi

A one-liner. ( The red 3 shows where to put the order number of the desired line. )
Code:
perl -n -e '$n=[red]3[/red];' -e '$l[$.%$n]=$_;END{print$l[($.+1)%$n]}' /input/file

Feherke.
 
Use the Tie::File module and you can access the lines of a file like an array. The third to last line would be index -3 in the tied array.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you for all the applies! it worked for me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top