CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Using of sed

 



ticketE
Novice

Mar 20, 2013, 10:54 AM

Post #1 of 4 (464 views)
Using of sed Can't Post

Hi,
I want to replace in my text a string which synonim is already present in my hash %synonimous, but my code doesn't function.
What is it wrong?


Code
foreach my $key (keys %synonimous) { 
`sed -e 's/$key/$synonimous{$key}/' $filename1` > $filename1;
}



BillKSmith
Veteran

Mar 20, 2013, 12:23 PM

Post #2 of 4 (458 views)
Re: [ticketE] Using of sed [In reply to] Can't Post

Your code does not work because the second back-tic is in the wrong place.

I would not recommend fixing it. Back-tics are used when you need the standard output. In this case, it is already redirected to a file. The function system would be a better choice.

Better yet, implement your editing in perl.


Code
  
#!perl -p
use strict;
use warnings;
BEGIN{
use vars qw(%SYNONYMS $SOURCE);
%SYNONYMS = (
old1 => 'new1',
old2 => 'new2',
old2 => 'new3',
);
my $source = join '|', keys %SYNONYMS;
$SOURCE = qr/$source/;
}
s/($SOURCE)/$SYNONYMS{$1}/e;


USAGE:

Code
perl script.pl infile > outfile

Good Luck,
Bill


ticketE
Novice

Mar 21, 2013, 7:56 AM

Post #3 of 4 (448 views)
Re: [BillKSmith] Using of sed [In reply to] Can't Post

My problem is that I have to do all in one script and execute this with a command:
./script.perl

It's not correct for my application, I can't change the content of the hash, I only need to find and replace, using my hash and a file of input/output.


BillKSmith
Veteran

Mar 21, 2013, 9:31 AM

Post #4 of 4 (445 views)
Re: [ticketE] Using of sed [In reply to] Can't Post

I do not think that you understood my code. I have made minor changes to try and answer your objections. Readonly is not required. I use it to prove that neither the hash nor Regex is ever modified.

You seem to be using UNIX. I changed the #! line to the UNIX default. The -p runtime option (on that line) handles all I/O and looping much like sed.

The perl substitution operator is much more powerful than sed's. Looping through all the keys is done by the Regex itself. The match is stored in the special variable $MATCH (Refer: perldoc perlvar). Note the /e on the operator. It tells perl to eval the resulting hash expression before making the final substitution.


Code
#!\usr\local\bin\perl -p 
# Note the runtime option -p (Refer: perldoc perlrun)
use 5.010; # Needed for \\=
use strict;
use warnings;
use Readonly;
use English;
BEGIN{
$ARGV[0] //= 'input_file_name'; # Set default file name
use vars qw( %SYNONYMS $SOURCE ); # Needed outside scope of BEGIN
Readonly::Hash %SYNONYMS => ( # Make sure hash is never changed
old1 => 'new1',
old2 => 'new2',
old3 => 'new3',
);
my $source = join '|', keys %SYNONYMS;
Readonly::Scalar $SOURCE => qr/\b$source\b/o; # Compile Regex only once
}
s/$SOURCE/$SYNONYMS{$MATCH}/eg; # Edit every line (Each line in $_)

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives