简体   繁体   English

使用sed或awk用单引号替换反引号

[英]Replace backtick with single quote using sed or awk

I want to replace all backticks ( ´ ) with single quotation marks ( ' ) in specific text document using sed or awk inside of PHP shell_exec() without using hexadecimal or octal code. 我想,以取代所有的反引号( ´ )用单引号( ' PHP了shell_exec(),而不使用十六进制或八进制代码使用awk或者sed内)在特定的文本文档。

At first, I tried these commands running inside of shell script: 首先,我尝试在shell脚本中运行这些命令:

sed -e 's/´/'"'"'/g' file.txt;
sed -e 's/\´/'"'"'/g' file.txt;
sed -e 's/´/'/g' file.txt;
sed -e 's/\´/'/g' file.txt;
sed -e 's/"´"/'"'"'/g' file.txt;
awk '{gsub(/\´/, "\'" ) }1' file.txt;

but none of these worked. 但这些都没有奏效。

Example of input.txt file: input.txt文件的示例:

abc´def´123`foo'456;

Example of output.txt file: output.txt文件的示例:

abc'def'123`foo'456

Using sed command running from terminal this solution works: 使用从终端运行的sed命令,此解决方案有效:

echo "a´b´c;" | sed "s/´/'/g"

and output is: 输出是:

a'b'c;

but running it from executable file test.sh: 但是从可执行文件test.sh运行它:

#!bin/bash
sed "s/´/'/g" input.txt > output.txt

as shell script executed by command 作为命令执行的shell脚本

bash test.sh

it doesn't work and content of output.txt file is same as content of input.txt file. 它不起作用,output.txt文件的内容与input.txt文件的内容相同。

However, with forward tick it works and result of 然而,随着前锋滴答它的工作和结果

#!bin/bash
sed "s/\`/'/g" input.txt > output.txt

is output.txt file with content 是带有内容的output.txt文件

abc´def´123'foo'456;

However, when I try to replace backward ticks with single quotation marks using hex or octal representation of characters it works like a charm. 但是,当我尝试使用单引号替换后向刻度时使用字符的十六进制或八进制表示,它就像一个魅力。

Using sed: 使用sed:

input.txt  - abc´def´123`foo'456;
command    - sed 's/\xB4/'"'"'/g' input.txt > output.txt;
output.txt - abc'def'123`foo'456

Using awk: 使用awk:

input.txt  - abc´def´123`foo'456;
command    - awk '{gsub(/\264/, "\047" )}1' input.txt > output.txt;
output.txt - abc'def'123`foo'456

Problem is, that I'm using another script applied to the document with mentioned script that replaces every hexadecimal or octal code with its literal representation. 问题是,我正在使用应用于文档的另一个脚本,其中提到的脚本用其文字表示替换每个十六进制或八进制代码。 I'm able to do it another way, I'm just curious if mentioned replacement can be used without using hex or octal code. 我能够以另一种方式做到这一点,我只是好奇如果提到替换可以使用而不使用十六进制或八进制代码。

You can use tr with appropriate quoting: 您可以使用tr适当的引用:

s='abc´def´123´foo'

tr '´' "'" <<< "$s"
abc'def'123'foo

Running tr on a file: 在文件上运行tr

tr '´' "'" < file

I don't know about calling scripts from php but did you try just: 我不知道从php调用脚本,但你只是尝试:

sed "s/´/'/g" file.txt

Look: 看:

$ echo "a´b" | sed "s/´/'/g"
a'b

I think you're using the wrong tick since the tr solution you say works is replacing a different tick from the one you asked to replace in your question (forward tick vs backward tick). 我认为你使用的是错误的勾号,因为你说的有效的解决方案正在替换你在问题中要求替换的一个不同的刻度(向前刻度与向后刻度)。 If so then you WOULD need to escape the backtick: 如果是这样,那么你将需要逃避反击:

sed "s/\`/'/g" file.txt 

$ echo "a\`b" | sed "s/\`/'/g"
a'b

What are you calling shell functions inside php ? 你在php里面调用shell函数是什么? php has str_replace function that can do this job for your. phpstr_replace函数,可以为你做这个工作。

<?php
echo str_replace("`","'","He`lo w`rld!");
?>

You could use Bash: 你可以使用Bash:

while read line; do
    echo ${line//\´/\'}
done < file.txt

这可能适合你(GNU sed):

sed 'y/`/'\''/' file

尝试这个:

sed "s/\`/'/" file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM