2

How to search and remove

6 ./ABC/DEG/gmail/xyz.txt
39415 ./ABC/DEG/yahoo/xyz.mp3

goal is to search and remove "/ABC/DEG"

Perl script
use strict;
use warnings;

my $infile = 'file.txt';

local @ARGV = ($infile);
local $^I = '.bac';
while( <> ){
    s/ABC/DEG//;
    print;
}
Mihir
  • 557
  • 1
  • 6
  • 28
  • Also see : http://stackoverflow.com/questions/18998180/inplace-replacement-in-a-file-through-a-perl-script – sgauria Mar 17 '14 at 18:20

2 Answers2

3

You could do this directly from your terminal:

perl -pi -e 's{ABC/DEG}{}g' <filename>

or if you still want a backup:

perl -pi.bak -e 's{ABC/DEG}{}g' <filename>
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • thanks, but getting error "The syntax of the command is incorrect" – Mihir Mar 17 '14 at 18:19
  • @Mihir my mistake, I thought you wanted to replace `ABC` with `DEG`. To replace `ABC/DEG` with nothing you can either 1) escape the forward slash in your replacement or 2) use a different delimiter. – Hunter McMillen Mar 17 '14 at 18:20
3

You can either escape the forward slashes, or use different delimiters: s{}{}

use warnings;
use strict;

while (<DATA>) {
    s{/ABC/DEG}{};
    print;
}

__DATA__
6 ./ABC/DEG/gmail/xyz.txt
39415 ./ABC/DEG/yahoo/xyz.mp3
toolic
  • 57,801
  • 17
  • 75
  • 117