The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 06:45 PM
Multile Pattern Search in a same line and delete sasree76 Shell Programming and Scripting 2 04-16-2008 02:12 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
Perl: Search for string then parse next line pondlife Shell Programming and Scripting 4 04-06-2006 04:44 AM
Pattern occurence in a file videsh77 UNIX for Dummies Questions & Answers 5 12-10-2004 07:41 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-30-2008
ammu ammu is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 79
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
  #2 (permalink)  
Old 01-30-2008
lucaswoods lucaswoods is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 9
perl -ne 'while(<>) {if(/^cool/) { $x = $_; }} print $x;'
  #3 (permalink)  
Old 01-30-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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 08:44 PM.. Reason: had to add "exit"
  #4 (permalink)  
Old 01-30-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

Similar discussion at Need a script to remove last comma in a file - LinuxQuestions.org ... cheers, drl
  #5 (permalink)  
Old 01-31-2008
lijju.mathew lijju.mathew is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 10
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0