如何在Unix中替换撇号字符

时间:2013-06-12 09:07:36

标签: unix replace sed character

我的文件包含撇号()。如果文件在Windows中打开但是如果文件在Unix中打开则无法看到它,我能够看到此字符。但是我需要在删除该字符后使用该文件一次。

我无法使用Windows手动删除该字符。我的服务器是Unix,所以我需要在那时删除该字符。我尝试了以下但是没有用。

cat HAllResponses_11004*.txt| sed 's/’/'/g;'>HAllResponses_11004_1.txt
  1. 如果没有出现该角色,如何识别该角色。
  2. 如果该角色没有出现,如何替换该角色。

  3. 你的3个答案没有帮助我解决这个问题。 当我使用六进制值时,它给出如下。

    $ echo -e“compilin \ xe2 \ x80 \ x99我的程序”

    编译我的程序

    问题是当我在unix中粘贴撇号时,它显示为'。'

    Pl帮帮我

3 个答案:

答案 0 :(得分:1)

字符与'字符不同。为了更清楚地看到它,检查它们的十六进制值:

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

答案 1 :(得分:1)

您可以使用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.$

答案 2 :(得分:0)

您可以使用以下内容查找 - 使用'替换sed(请注意,您需要转义特殊字符)

$ cat a.txt
This line don't have a '
This is test

$ sed s/\'//g a.txt
This line dont have a
This is test

$ sed s/\'/\"/g a.txt
This line don"t have a "
This is test

如果要就地编辑文件,可以使用以下语法(请注意,执行命令后将修改a.txt的内容)

$ sed -i s/\'/\"/g a.txt