Perl comand在unix中将关闭的单引号转换为单引号
我已经创建了一个文件。给出了一个场景。 包含记录Perl comand在unix中将关闭的单引号转换为单引号,perl,shell,unix,awk,sed,Perl,Shell,Unix,Awk,Sed,我已经创建了一个文件。给出了一个场景。 包含记录'RAZ'。这里是关闭单引号,我想同时用单引号'RAZ'全局替换它 如果关闭单引号是在某个字的中间,我不想替换 例如: 我试着服从命令 sed -i "s/\’/'/g" Test.txt 输出: 'RAZ'|Rockpor't 所需输出应为: 'RAZ'|Rockpor`t 请告知。您可以这样做 您的输入文件,比如/tmp/abc.txt #cat /tmp/abc.txt `ABC`|REsaff`t|`rest` `PQR`|sasd
'RAZ'
。这里是关闭单引号,我想同时用单引号'RAZ'
全局替换它
如果关闭单引号是在某个字的中间,我不想替换
例如:
我试着服从命令
sed -i "s/\’/'/g" Test.txt
输出:
'RAZ'|Rockpor't
所需输出应为:
'RAZ'|Rockpor`t
请告知。您可以这样做 您的输入文件,比如/tmp/abc.txt
#cat /tmp/abc.txt
`ABC`|REsaff`t|`rest`
`PQR`|sasdasd`y|d`souza
现在,运行这个命令
cat /tmp/abc.txt | sed s?\|?\\n\|?g | sed s?^\`?\'?g | sed s?\`\$?\'?g | sed s?^\|\`?\|\'?g | tr '\n' '#' | sed s?#\|?\|?g | tr '#' '\n'
该命令所做的是,它隔离所有选项卡值,如果选项卡的值以“开始”和“结束”,则替换为“,然后再次将所有选项卡合并在一起
期望输出:
'ABC'|REsaff`t|'rest'
'PQR'|sasdasd`y|d`souza
此解决方案可用于任意数量的选项卡。如果字符位于选项卡的开头或结尾,则所有选项卡中的字符`都将被替换
注意:我在这里使用的delimeter是
#
来分隔换行符,您可以相应地使用任何换行符。使用下面的代码。希望有帮助
sed 's/`/'"'"'/;s/`/'"'"'/' test.txt
我使用“退出单引号功能。请参阅下面的屏幕截图。graditude”
此perl命令将为您完成此任务
perl -pe "s/`(?![a-z])|(?<![a-z])`/'/ig"
我建议
perl -pe 's/(^|\W)`(.*?)`($|\W)/$1\x27$2\x27$3/g'
扩大
perl -pe '
$q = "\x27"; # a single quote
s{
(^|\W) # start of line or a non-word char
` # the open quote
(.*?) # some non-quote characters
` # the close quote
($|\W) # end of line or a non-word char
}{$1$q$2$q$3}gx
'
演示
您在问题文本('U+0060)中使用的引号与示例数据('U+2019)中使用的引号不同。
perl -pe 's/(^|\W)`(.*?)`($|\W)/$1\x27$2\x27$3/g'
perl -pe '
$q = "\x27"; # a single quote
s{
(^|\W) # start of line or a non-word char
` # the open quote
(.*?) # some non-quote characters
` # the close quote
($|\W) # end of line or a non-word char
}{$1$q$2$q$3}gx
'
perl -pe '$q="\x27"; s/(^|\W)`(.*?)`($|\W)/$1$q$2$q$3/g' <<'END'
`foo`|I have James` hat|doesn`t
END
'foo'|I have James` hat|doesn`t