in reply to Yet another substitution problem
s/// stands for substitution operation, not sed. sed(1) is not synonymous to substitution, even though that may be its most used feature. Could you please update the title of your post to "2 substitution problems"?
/vobs/dirname1////dirname2//dirname3////filename becomes /vobs/dirname1/dirname2/dirname3/filename
I have tried to replace 2 or more "/"'s with a single "/" using
$filename =~ s/\/[\/]+/\//;but the above statement does nothing
That last statement is false, which you would have noticed if you had printed the file name before & after s/// operation. You would have seen that at least first set of consecutive '/' is squashed to single one. To flatten all the rest, all you need to do is globally replace by using /g flag ...
# Used a delimiter other than '/' to avoid escaping '/'. $p =~ s!//+!/!g;
As for your second problem, the subdirectory needs to be captured to reference in the pattern (and later in substitution) ...
$p = '/vobs/synergy_core_apps/code/../code/personalize/src'; print 'before: ' , $p , "\n"; # Besides the /g flag, used /x in order to make the pattern stand out +. $p =~ s{ ([^/]+) /[.]{2}/ \1 } /$1/gx; print 'after: ' , $p , "\n";
If the paths being cleansed actually exist on the file system, I would rather use &Cwd::realpath.
See also: perlretut, perlre, YAPE::Regex::Explain, perlfaq6, Mastering Regular Expressions, etc.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: 2 sed problems
by AnomalousMonk (Archbishop) on Mar 28, 2009 at 15:23 UTC | |
by parv (Parson) on Mar 28, 2009 at 16:24 UTC | |
by AnomalousMonk (Archbishop) on Mar 28, 2009 at 22:11 UTC | |
by parv (Parson) on Mar 29, 2009 at 03:04 UTC |