LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2008, 11:32 AM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
substitution question in perl


Let's say I have a line of text as follows;

"_sometext/somemore_text/abc>, Output File <some_more_text "

Using perl I want to remove "Output File" and everything that follows. I have tried 's/Output File*//g' without success. It only removed 'Output File' and left all following text as is.

I thought * meant 'everything that follows. Apparently I was wrong. Any comments advice or suggestions is welcome.

thanks,
 
Old 11-03-2008, 12:15 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by sharky View Post
Let's say I have a line of text as follows;

"_sometext/somemore_text/abc>, Output File <some_more_text "

Using perl I want to remove "Output File" and everything that follows. I have tried 's/Output File*//g' without success. It only removed 'Output File' and left all following text as is.

I thought * meant 'everything that follows. Apparently I was wrong. Any comments advice or suggestions is welcome.

thanks,
s/\bOutput\s+File.*//
 
Old 11-03-2008, 12:17 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

the "*" in Perl says: the previous character or expression 0 .. n times. So if you want to remove everything from "Output File" to the end of line you have to use: "s/Output File.*//" - the "." is the expression for any character.

Jan
 
Old 11-03-2008, 03:09 PM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Thanks. It was the "." culprit.
 
Old 11-03-2008, 07:10 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
"At first blush," it seems to me that you were hoping that the semantics of the ls command would somehow work here.

Nope. Sorry.

When you're contemplating any situation where (you know...) "a regular expression will really help me here," bear in mind that there are two things which regex-based logic fundamentally needs to know:
  1. "What place-markers will tell me that 'I am in the right neighborhood(s)?'"
  2. (Then... "now that I'm here...") "What will identify 'what I need to isolate?, perchance to change?'"
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl regex substitution lrt Programming 3 12-08-2007 07:55 AM
Perl substitution with arrays cramer Programming 2 08-14-2006 11:07 PM
Substitution in Perl rigel_kent Programming 4 06-02-2006 10:11 AM
Variable substitution (stupid noob) question in tcsh King of Men Linux - Newbie 4 12-04-2005 09:12 AM
shell question variable substitution dpressm Programming 9 11-18-2004 07:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration