how to replace ’ with ’ in perl [closed] - regex

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have below piece of code in Perl
my $file2 = "devil’s-claw-extract.html";
$file2 =~ s/’/’/ig;
print "$file2";
This code is working fine(means replacing ’ with ’) when running in konsole but not working in browser.
Please help me out.

Of course it “works”, and I will believe so until you produce a self-contained example that indicates otherwise.
Your first problem is that you are reinventing the wheel, there already is a module on CPAN to do such escaping for you:
use utf8; # because this source file contains special chars
use HTML::Entities;
my $file2 = "devil’s-claw-extract.html";
print encode_entities $file2;
Output:
devil’s-claw-extract.html
or with encode_entities_numeric:
use utf8;
use HTML::Entities 'encode_entities_numeric';
my $file2 = "devil’s-claw-extract.html";
print encode_entities_numeric $file2;
Output:
devil’s-claw-extract.html
Secondly, it is worth noting that your input string contains ’, a single right quote. This has the codepoint U+2019 (not U+0092, which is in a private use area. Conveniently, it decodes to ’ in the Windows-1252-encoding, but the actual encoding should always be explicitly set).
The apostrophe ', which you likely wanted, is U+0027 or '.

Related

Replace the words "can't, don't" by "can not, do not" using python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need to replace words like "{can't, don't, won't }" by "{can not, do not, would not}" using python
The problem is:
"can't" can be detected by checking suffix "n't", so we can replace "n't" by "not"
But how can we transform "ca" to "can" as when we split "can't" it should be transformed to "can not"?
Since the rules of English are large and sometimes inconsistent, your best bet is probably just to set up full word maps rather than trying to figure out on the fly which letters are represented by the apostrophe.
In other words, a dictionary with values like:
can't -> can not
don't -> do not
won't -> will not
:
oughtn't -> ought not

unix change number's value on given line number in shell script for loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can I change a number on a line in a file using a unix tool like awk or sed?
I want to change the line 3 in my example file to the number 1-10 using a shell script. I think I need to use regex to recognize the digit but I'm not sure how to do this, or to allow multiple digits (like 10).
Example file:
/examples/are/hard so/hard/1
Shell script so far:
for i in {1..3};
do
sed 's|/examples/are/hard so/hard/7 | /examples/are/hard so/hard/'"$i" ex_file
cat ex_file
done
Desired output:
/examples/are/hard so/hard/1
/examples/are/hard so/hard/2
/examples/are/hard so/hard/3
What you've run isn't a valid sed command. If you're trying to do a substitution, that's s/search/replace/flags.
I imagine you meant:
sed 's/here\/is the number\/to\/change 3/here\/is the number\/to\/change '"$i"'/' ex_file
Note that we temporarily break out of single quote. Inside of single quotes, variable aren't interpolated. We swap the double quotes, bring in $i, then return to single quotes to finish the command.
P.S. You also don't have to use / as your delimiter.
sed 's|here/is the number/to/change 3|here/is the number/to/change '"$i"'|' ex_file

Replacing ascii with matching characters using perl / awk or other [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an access log that was written with nginx and lua code.
It is url encoded and those some characters are written in the format of \xHexCode (for example, double quotes are written as \x22).
I would like to run awk or perl or other fast script to replace it back.
You can use gnu-awk like this:
str='\x22 \x41 written as \x22).'
awk -v RS='\\\\x[0-9]+' 'RT{ORS=sprintf("%c", strtonum("0" substr(RT, 2)))} 1' <<< "$str"
" A written as ").
This is how it is working:
Using RS='\\\\x[0-9]+' we're separating custom record separator for each of those \xNN numbers.
substr(RT, 2) takes x41 from \x41
strtonum("0" substr(RT, 2)) adds 0 to make it 0x41 and returns ascii code 65.
printf "%c" prints equivalent ascii character A from 65.
ORS=... sets output record separator same as the return value of sprintf.

How to check the format of keyboard input for a Perl script? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My Perl script just takes whatever you give as an input, and I want make it more robust by checking the pattern of the input string. My input string has to be in the format xxxxx-xxxx-xxxx. How can I check that?
$foo =~ /^.{5}-.{4}-.{4}\z/s
For example, this will repeatedly ask for the value until it gets a valid one.
my $foo;
while (1) {
print("Please provide foo (xxxxx-xxxx-xxxx): ");
my $foo = <STDIN>;
die("EOF\n") if !defined($foo);
chomp($foo);
last if $foo =~ /^.{5}-.{4}-.{4}\z/s;
print("Invalid input\n");
}

Doing SSH via PERL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to login to a different shell in a different server. So i wrote the following code.
#!/volume/perl/bin/perl
use lib qw(/volume/labtools/lib);
use Net::SSH::Perl;
my $host = 'wd-shell2';
my $cmd = "cd /volume/ftp/private/det/os;ls -lrt jinstall*";
my $user = 'joydeep';
my $pass = '';
my $ssh = Net::SSH::Perl->new("$host", debug=>0);
$ssh->login($user, $pass);
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
print "\n$stdout\n";
I am not sure, whether i wrote it right or not. i am in tts-shell1, how can i login to wd-shell2 and do my thing with the code(after correcting). Please help me here.
First off, always include use strict; and use warnings; at the top of EVERY perl script.
Secondly, you should turn on debug mode so the module will give you as many helpful messages as possible:
my $ssh = Net::SSH::Perl->new("$host", debug => 1);