Is there a way to prevent sed from adding carriage

2019-09-10 12:07发布

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;

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');

Using sed (GNU sed) 4.2.2, Ubuntu 16.04

Here is the screenshots for clarify the issue:

enter image description here

enter image description here

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.

3条回答
Lonely孤独者°
2楼-- · 2019-09-10 13:03

Try to convert line ending from DOS format to unix format :

sed 's/^M$//' $full_path/wp-config.php > $full_path/wp-config.php

More methods here : http://www.cyberciti.biz/faq/howto-unix-linux-convert-dos-newlines-cr-lf-unix-text-format/

查看更多
ら.Afraid
3楼-- · 2019-09-10 13:06

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.

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

In short, the issue is not with sed, it's with the original file.

查看更多
SAY GOODBYE
4楼-- · 2019-09-10 13:09

Tested, can't replicate.

The file does contain windows line endings to begin with in Wordpress itself. (see the sample file).

Running the command posted (number 2), results in

define('WP_DEBUG', false);                                                      
define('WP_MEMORY_LIMIT', '96M');^M

which is expected behaviour, which, while not the output in OP's original question, is exactly what they show they get in their screenshots.

查看更多
登录 后发表回答