有没有一种方法可以防止sed添加回车符(^ M)? [英] Is there a way to prevent sed from adding carriage return (^M)?
问题描述
我正在尝试在wordpress php文件中的 define('WP_DEBUG',false);
之后添加 define('WP_MEMORY_LIMIT','96M');
.
I am trying to add define('WP_MEMORY_LIMIT', '96M');
after the define('WP_DEBUG', false);
in a wordpress php file.
这是我到目前为止尝试过的:
Here is what I tried so far:
1-
sed -b -i "/'WP_DEBUG', false);/a define('WP_MEMORY_LIMIT', '96M');" $full_path/wp-config.php;
2-
sed -i "s/'WP_DEBUG', false);/'WP_DEBUG', false);\ndefine('WP_MEMORY_LIMIT', '96M');/" $full_path/wp-config.php;
问题在于,用该回车替换的所有新行都返回char.如何在特定行之后添加新行而没有这个问题?
The problem with that, all the new lines being replaced with this carriage return char. How can I add a new line after a specific line and do not have this issue ?
define('WP_DEBUG', false);^M
define('WP_MEMORY_LIMIT', '96M');
使用sed(GNU sed)4.2.2,Ubuntu 16.04
Using sed (GNU sed) 4.2.2, Ubuntu 16.04
以下是用于澄清问题的屏幕截图:
Here is the screenshots for clarify the issue:
注意:Okey,阅读@anishsane的答案后问题解决了.由于原始文件(来自wordpress.org/latest.zip)具有CRLF(windows)行结尾,因此添加\ n会破坏文件视图.使用"\ r \ n"解决了该问题:
NOTE: Okey, problem is solved after reading @anishsane's answer. Since the original file (from wordpress.org/latest.zip) has CRLF (windows) line endings, adding \n was breaking the file view. Using "\r\n" solved the issue:
sed -i "s/'WP_DEBUG', false);/'WP_DEBUG', false);\r\ndefine('WP_MEMORY_LIMIT', '96M');/" $full_path/wp-config.php;
我不确定为什么要投票.请解释,以便我澄清这个问题.
I am not sure why the downvotes. Please explain, so I can clarify the question.
推荐答案
该文件最初具有CRLF行尾.当您在 vim
编辑器中打开文件时,vim会知道该文件的CRLF结尾是& ;;从用户隐藏.通过编辑器添加的任何新行也将具有与文件其余部分相同的行尾.
The file originally has CRLF line endings. When you open it in vim
editor, vim understands that file has CRLF endings & hides them from user. Any new line/s added via the editor will also have the same line endings as the rest of the file.
通过 sed
添加新行时,它的行尾为 LF
.下次当您在 vim
中打开它时,vim会看到混合的行尾, CRLF
& LF
.然后, vim
决定将其解释为具有 LF
行尾的文件.&所有 CR
字符都突出显示为 ^ M
.
When you add a new line via sed
, it has LF
line endings. Next time when you open it in vim
, vim sees mixed line endings, CRLF
& LF
. vim
then decides to interpret it as file with LF
line endings. & all CR
characters are highlighted as ^M
.
要测试,请尝试以下操作:
to test, try this:
$ printf '%d\r\n' {1..5} > test_endings # This will create a file with CRLF endings.
$ file test_endings
test_endings: ASCII text, with CRLF line terminators
$ vim test_endings
1
2
3
4
5
~
~
"test_endings" [dos] 5L, 15C <~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Notice the word DOS here.
$ echo 6 >> test_endings # This will add line with LF line endings.
$ file test_endings
test_endings: ASCII text, with CRLF, LF line terminators
$ vim test_endings
1^M
2^M
3^M
4^M
5^M
6
~
~
"test_endings" 6L, 17C
简而言之,问题不在于 sed
,而是原始文件.
In short, the issue is not with sed
, it's with the original file.
这篇关于有没有一种方法可以防止sed添加回车符(^ M)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!