Unix中如何替换撇号字符
我的文件包含一个撇号(Unix中如何替换撇号字符,unix,replace,sed,character,Unix,Replace,Sed,Character,我的文件包含一个撇号(”)。如果文件在Windows中打开,我可以看到这个字符,但是如果文件在Unix中打开,我就看不到这个字符。但我需要在删除该字符后使用该文件一次 我无法使用windows手动删除角色。我的服务器是Unix,因此我需要删除该字符。我试过下面的方法,但没用 cat HAllResponses_11004*.txt| sed 's/’/'/g;'>HAllResponses_11004_1.txt 如果该字符未出现,如何识别该字符 如果该字符不出现,如何替换该字符 你的
”
)。如果文件在Windows中打开,我可以看到这个字符,但是如果文件在Unix中打开,我就看不到这个字符。但我需要在删除该字符后使用该文件一次
我无法使用windows手动删除角色。我的服务器是Unix,因此我需要删除该字符。我试过下面的方法,但没用
cat HAllResponses_11004*.txt| sed 's/’/'/g;'>HAllResponses_11004_1.txt
你的三个答案并没有帮助我解决这个问题。 当我使用hexa值时,它给出如下 $echo-e“compilein\xe2\x80\x99我的程序” compilein–我的程序 问题是,当我在unix中粘贴撇号时,它显示为“.”
Pl help me您可以使用以下命令使用
sed查找replace
(请注意,您需要转义特殊字符)
如果要就地编辑文件,可以使用以下语法(请注意,a.txt
的内容将在执行命令后修改)
'
字符与'
字符不同。要更清楚地了解这一点,请检查其十六进制值:
echo -n ’ | hexdump -C
00000000 e2 80 99 |...|
00000003
echo -n \' | hexdump -C
00000000 27 |'|
00000001
现在,当使用sed
或类似工具在序列中替换时,可以通过其十六进制值来识别”
:
echo -e "compilin\xe2\x80\x99 my program"
compilin’ my program
echo -e "compilin\xe2\x80\x99 my program" | sed "s|\xe2\x80\x99|'|"
compilin' my program
仅当撇号字符出现在文本中时,它才会替换撇号字符。在您的情况下,只需将文件名作为第二个参数传递给sed
,就完成了:
sed -i "s|\xe2\x80\x99|'|" HAllResponses_11004_1.txt
或者只是:
sed -i "s|’|'|" HAllResponses_11004_1.txt
您可以使用cat-vet查看unix中的控制字符,然后使用sed替换这些字符。在下面的示例中,cat-vet将(')显示为(M-^R),可以使用sed轻松替换
原始文件:
My file contains an apostrophe (’). I am able to see this character if the file opens in Windows but not able to see it if the file opens in Unix. But I need to use that file once after removing that character.
I can't use windows to remove the character manually. My servers are Unix so I need to remove the character at that point. I have tried the following but it didn't work.
在Unix中使用cat-vet显示的控制字符:
/home/temp_files > cat -vet SO.txt
My file contains an apostrophe (M-^R). I am able to see this character if the file opens in Windows but not able to see it if the file opens in Unix. But I need to use that file once after removing that character.$
I can't use windows to remove the character manually. My servers are Unix so I need to remove the character at that point. I have tried the following but it didn't work.$
替换为sed:
/home/temp_files > cat -vet SO.txt | sed 's/M-^R//g'
My file contains an apostrophe (). I am able to see this character if the file opens in Windows but not able to see it if the file opens in Unix. But I need to use that file once after removing that character.$
I can't use windows to remove the character manually. My servers are Unix so I need to remove the character at that point. I have tried the following but it didn't work.$
你的三个答案并没有帮助我解决这个问题。当我使用hexa值时,它给出的值如下所示。$echo-e“compilein\xe2\x80\x99 my program”compilein–my program问题是当我在unix中粘贴撇号时,它显示为“.”Pl help meDid您是否尝试了我的建议?
/home/temp_files > cat -vet SO.txt
My file contains an apostrophe (M-^R). I am able to see this character if the file opens in Windows but not able to see it if the file opens in Unix. But I need to use that file once after removing that character.$
I can't use windows to remove the character manually. My servers are Unix so I need to remove the character at that point. I have tried the following but it didn't work.$
/home/temp_files > cat -vet SO.txt | sed 's/M-^R//g'
My file contains an apostrophe (). I am able to see this character if the file opens in Windows but not able to see it if the file opens in Unix. But I need to use that file once after removing that character.$
I can't use windows to remove the character manually. My servers are Unix so I need to remove the character at that point. I have tried the following but it didn't work.$