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

Specific search & replace text

Status
Not open for further replies.

avu

Technical User
Aug 8, 2003
53
0
0
CA
Hi,
Could someone please help me with a perl script to search & replace a text string such that:

$org_str = "C:/root/sub/text/somefile.txt"
to become: "C:/root/sub/somefile.html"

Basically: "/text/*.txt" will become "/*.html"

Much appreciated for your help.

 
Sorry, the requirement is a bit more tricky:

$origStr = "C:/root/sub/level/text/somefile.txt"
to become: "C:/root/sub/level/somefile.html"

(remove "text/" and replace ".txt" with ".html")

And it only applies to strings that contains "/sub/", that is, for example, "C:/root/any_other_sub/level/text/*.txt" does not apply.

Thanks.
 
Assuming that what you really want to do is file system operations and not just string search and replacement, then this is what you are searching for:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Basename[/green] [red]qw([/red][purple]fileparse[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]File::Spec::Functions[/green] [red]qw([/red][purple]catdir catfile[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$basedir[/blue] = [red]'[/red][purple]C:/root/sub[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$textdir[/blue] = [maroon]catdir[/maroon][red]([/red][blue]$basedir[/blue], [red]'[/red][purple]text[/purple][red]'[/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR, [blue]$textdir[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't opendir [blue]$textdir[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$file[/blue] [red]([/red][url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url][red]([/red]DIR[red])[/red][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$name[/blue], [url=http://perldoc.perl.org/functions/undef.html][black][b]undef[/b][/black][/url], [blue]$suffix[/blue][red])[/red] = [maroon]fileparse[/maroon][red]([/red][blue]$file[/blue], [red]qr{[/red][purple][purple][b]\.[/b][/purple][^[purple][b]\.[/b][/purple]]*[/purple][red]}[/red][red])[/red][red];[/red]
	
	[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [blue]$suffix[/blue] ne [red]'[/red][purple].txt[/purple][red]'[/red][red];[/red]
	
	[black][b]my[/b][/black] [blue]$from[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$textdir[/blue], [blue]$file[/blue][red])[/red][red];[/red]
	[black][b]my[/b][/black] [blue]$to[/blue]   = [maroon]catfile[/maroon][red]([/red][blue]$basedir[/blue], [red]"[/red][purple][blue]$name[/blue].html[/purple][red]"[/red][red])[/red][red];[/red]
	
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$from[/blue] -> [blue]$to[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url][red]([/red]DIR[red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Basename - Parse file paths into directory, filename and suffix.[/li]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
[/tt]

Note: I purposefully demonstrated a system independent version of this type of translation.

- Miller
 
Assuming, unlike Miller's very nice script, that you're doing replacements in a variable as you say, try something like this.

if($SomeString =~ /sub/){ # if the string contains 'sub'
$SomeString =~ s/\.txt$/.html/; ' replace '.txt'
}
print $SomeString;


Mike

When working on any project the value of other people is exactly that - they are other people, with views that don't necessarily match yours. This mismatch, between their views and the view you've been contentedly assuming is right, is where that value lies.
 
Thanks Miller & Mike. The solution I was looking for indeed lied somewhere between string's search/replace & file system ops. It was for a web search result, in which links to text files were to be replaced by html files.

I tried to implement Miller's solution but unable to on a shared hosting environment, so I settled with search/replace string (Mike's solution.)

Thanks again for your help.

- Allan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top