我想用大引号 ( ‘ ’
, “ ”
) 替换直单引号和双引号。我如何使用 shell 命令来做到这一点?
答案1
使用此输入文件(由伯恩斯):
$ cat file
When 'chapmen billies' lea "the street" and 'drouthy' neebors "neebors" meet.
和任何 awk:
awk '{
while ( match($0,/\047[^\047]*\047/) ) {
$0 = substr($0,1,RSTART-1) "‘" substr($0,RSTART+1,RLENGTH-2) "’" substr($0,RSTART+RLENGTH)
}
while ( match($0,/"[^"]*"/) ) {
$0 = substr($0,1,RSTART-1) "“" substr($0,RSTART+1,RLENGTH-2) "”" substr($0,RSTART+RLENGTH)
}
print
}' file
When ‘chapmen billies’ lea “the street” and ‘drouthy’ neebors “neebors” meet.
上述假设您的报价始终成对出现。
答案2
在perl
UTF-8 语言环境中:
perl -C -0777 -pi.back -e '
s/\N{QUOTATION MARK}(.*?)\N{QUOTATION MARK}/\N{LEFT DOUBLE QUOTATION MARK}$1\N{RIGHT DOUBLE QUOTATION MARK}/gs;
s/\N{APOSTROPHE}(.*?)\N{APOSTROPHE}/\N{LEFT SINGLE QUOTATION MARK}$1\N{RIGHT SINGLE QUOTATION MARK}/gs
' your-files
(原始备份带有后缀.back
。.back
如果不关心备份,请删除)。