LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-15-2009, 01:01 PM   #1
ufmale
Member
 
Registered: Feb 2007
Posts: 385

Rep: Reputation: 30
pattern match anything before "dot"


I want to replace the filename name.exe with --FILE--.exe as part of my perl script.
Is there an easy way to do that with pattern matching?
Bascially, i want to match anything before ".exe", then replace them with "--FILE--".
 
Old 01-15-2009, 01:33 PM   #2
mk27
Member
 
Registered: Sep 2008
Distribution: fedora, gentoo, ubuntu
Posts: 148

Rep: Reputation: 23
use =~// to match
use =~s/// to substitute:
Code:
#!/usr/bin/perl -w 
use strict; 

while (<DATA>) {
	chomp $_; 	# remove newline
	if ($_=~/\.exe$/) {	# check if input ends with .exe
		$_=~s/^\w+/--FILE--/;  # replace alphanumeric content at beginning
	}
	print "$_\n";
}

__DATA__
file.exe
this.exe
notthis.txt
more.exe
Output:
--FILE--.exe
--FILE--.exe
notthis.txt
--FILE--.exe


\w is an alphanumeric character
+ is one or more of the character
\. is a real period (. is anything)
^ indicates the beginning
$ indicates the end

Google "Rex Swain's HTMLified Perl 5 Reference Guide"; it's a great, very short quick reference that includes all the regular expression stuff.

Also perlmonks.com is a very active and helpful forum
 
Old 01-15-2009, 01:35 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by ufmale View Post
I want to replace the filename name.exe with --FILE--.exe as part of my perl script.
Is there an easy way to do that with pattern matching?
Bascially, i want to match anything before ".exe", then replace them with "--FILE--".
You can use sed to do this, if you want ALL the files to be "--FILE--.exe".


$variable =~ s/<variable where your input comes from>/"--FILE--.exe"/;

Check the syntax, though...not sure that's right.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 07:17 AM
Regular expression matching , match "error string" but not "other error" jmcmillan Programming 3 07-07-2008 10:50 AM
A single regex to match anything with ".aac" or ".mp3" at the end ? lumix Linux - General 9 05-09-2008 02:11 AM
Why doesn't a wildcard chmod change "dot" files/directories? jht2k Linux - General 1 08-09-2004 03:31 PM
whys is that "df" and "du" dont match? jaredth Linux - Software 2 07-29-2004 02:23 AM


All times are GMT -5. The time now is 02:20 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration