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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Replacement

Status
Not open for further replies.

Newbee21369

Programmer
Oct 13, 2004
30
US
I'm trying to look for files that contain and ".". If so replace "." with * (wilcard) and remove all text after it.
for example if I have:

INPUT:
FileName1.Doc
FileName2.xls
FileName3.txt

OUTPUT:
FileName1*
FileName2*
FileName3*


Any Ideas on how I can do this?
Thanks in Advance.
 
You could use a regex for this, but I'd go with a split
Code:
($filename, $extension)=split /\./, $file;
$file.="*"; # the asterisk may need escaping if so try \*
HTH
--Paul

cigless ...
 
A split uses a regex anyway!
:-D
I'd go for the one liner:
Code:
perl -lne 's/\.*$/\*/;print' INPUT > OUTPUT


Trojan.
 
I agree with TrojanWarBlade. Sorry PaulTeg, but yours falls short if there is no "." in the name. Also, here is a correction to TrojanWarBlade's

Code:
s/\..*$/\*/;

There was a missing period to match any character and any numnber of characters after the ".".


Michael Libeson
 
what does this mean and where does file $file string fit into this? I forgot to mention I need to return a new string with the formatted file. Thanks again.
 
valid point, note to self, don't do this before coffee :D

cigless ...
 
Paul,
The extension will vary, how could this be written to take that in consideration?

($filename, $extension)=split /\./, $file;
$file.="*"; # the asterisk may need escaping if so try
 
What my code above does is split the value into two vars either side of the full stop/period.

So extensions are ignored, and the same is also true of the regexes provided by TWB and Mike, and the regex would by my preferred choice had I been sufficiently caffeinated ;-)

--Paul


cigless ...
 
This works!! Thanks!!!

($outfilename, $extension)=split /\./, $inputfile;
outfilename.="*";
 
Sorry guys,
I was in a rush to go out.
You're right, I missed the "."
:-$

Oh well, glad you picked up my deliberate mistake! ;-)

Thanks.

Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top