The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Search and replace entire line insania Shell Programming and Scripting 1 05-22-2008 03:45 PM
Multile Pattern Search in a same line and delete sasree76 Shell Programming and Scripting 2 04-16-2008 11:12 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 06:24 AM
Perl: Search for string then parse next line pondlife Shell Programming and Scripting 4 04-06-2006 01:44 AM
Pattern occurence in a file videsh77 UNIX for Dummies Questions & Answers 5 12-10-2004 03:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-30-2008
Registered User
 

Join Date: Jul 2006
Posts: 55
Stumble this Post!
Perl onliner to search the last line with an occurence of a pattern

Hi

I need a perl onliner which seaches a line starting with a pattern(last occurence) and display it.
similar to

grep 'pattern' filename | tail -1 in UNIX

Ex: I want to display the line starting with "cool" and which is a last occurence

adadfadafadf
adfadadf
cool dfadfadfadfara
adfadfadf
cool aaaaaaaaaaaaaa
hai how are you

It should dispaly the line which is red. I need it in perl one line command

I am not sure how i should do. I have an idea that we can do it with

perl -ne 'print .........

Your help will be highly appreciated

Thanks
Ammu
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 01-30-2008
Registered User
 

Join Date: Jan 2008
Posts: 9
Stumble this Post!
perl -ne 'while(<>) {if(/^cool/) { $x = $_; }} print $x;'
Reply With Quote
  #3 (permalink)  
Old 01-30-2008
Registered User
 

Join Date: Jan 2008
Posts: 306
Stumble this Post!
another suggestion:

Code:
perl -ne '@t = grep{/^cool/}<>; print $t[-1]; exit' path/to/inputfile
change single quotes to double-quotes if using Windows

Last edited by KevinADC; 01-30-2008 at 04:44 PM. Reason: had to add "exit"
Reply With Quote
  #4 (permalink)  
Old 01-30-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 505
Stumble this Post!
Hi.

Similar discussion at Need a script to remove last comma in a file - LinuxQuestions.org ... cheers, drl
Reply With Quote
  #5 (permalink)  
Old 01-30-2008
Registered User
 

Join Date: Apr 2007
Posts: 10
Stumble this Post!
In the above example i would like to add another line after the red line(last occurence of line starting with cool).How can i do that with a perl onliner

Thanks in advance

Lijju
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0