【问题标题】:How to replace apostrophe character in Unix如何在 Unix 中替换撇号字符
【发布时间】:2013-06-08 08:08:35
【问题描述】:

我的文件包含一个撇号 ()。如果文件在 Windows 中打开,我可以看到这个字符,但如果文件在 Unix 中打开,我看不到它。但我需要在删除该字符后使用该文件一次。

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

cat HAllResponses_11004*.txt| sed 's/’/'/g;'>HAllResponses_11004_1.txt
  1. 如果该字符未出现,如何识别该字符。
  2. 如果该字符不出现,如何替换该字符。

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

$ echo -e "编译\xe2\x80\x99 我的程序"

编译我的程序

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

请帮帮我

【问题讨论】:

  • 你尝试过我的建议了吗?

标签: unix replace sed character


【解决方案1】:

您可以使用以下内容来查找-替换' 使用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

【讨论】:

    【解决方案2】:

    字符与 ' 字符不同。要更清楚地看到这一点,请检查它们的十六进制值:

    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
    

    【讨论】:

    • 您的 3 个答案并没有帮助我解决问题。当我使用十六进制值时,它给出如下。 $ echo -e "compilin\xe2\x80\x99 my program" compilinâ my program 问题是当我在 unix 中粘贴撇号时,它显示为 '.'请帮帮我
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多