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

Multiple matches and replacements 1

Status
Not open for further replies.

cryptoadm

MIS
Nov 6, 2008
71
US
How would you do a match and replace for multiples in a variable?

Example:
PATH=$PATH:/home/userx:/usr/X11:.:/usr/sbin:..:/usr::/var:.

I need to remove any . and .. and :: in the path and also if any of those 3 characters are at the end of the line they need to be removed.

My code works to remove the . and .. and the first instance of :: but not any subsequent instances. Also cannot get the end of line match/replace to work.

Code:
while (<>) {
      chomp ($_);
      if ($_ =~ m/^PATH/) {
              s/\.//;
              s/\.\.//;
              s/::/:/;
              print;
      }
}
 
add the "g" modifier:

Code:
while (<>) {
      chomp;
      if (/^PATH/) {
              s/\.{1,2}//g;
              s/::/:/g;
              print;
      }
}

Look up the "g" modifier in any regexp tutorial for more details.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top