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.
In reply to Re: 2 substitution problems
by parv
in thread Yet another substitution problem
by Amphiaraus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |