0
4

3 回答 3

1
于 2013-06-12T12:58:14.983 回答
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.$
于 2013-06-12T14:23:25.053 回答
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
于 2013-06-12T11:48:09.133 回答