4

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.

mirza
  • 5,685
  • 10
  • 43
  • 73
  • 1
    GNU sed, on Ubuntu, adding Windows line endings? That seems unlikely. – Jonnix Jun 16 '16 at 09:06
  • http://prntscr.com/bh0y24 – mirza Jun 16 '16 at 09:07
  • So every line has one? So they were there before you were running sed? Get rid of them, then run sed, I'll bet they don't get added. Not that I can answer why it would be left in place... – Jonnix Jun 16 '16 at 09:09
  • no. you've got it wrong. http://prntscr.com/bh1020 – mirza Jun 16 '16 at 09:13
  • Are you sure that all those carriage returns aren't there before you run your command? It looks as though the line you've appended is the only one that doesn't have one. – Tom Fenech Jun 16 '16 at 09:13
  • 1
    I am sure. Check the screenshot http://prntscr.com/bh119k – mirza Jun 16 '16 at 09:16
  • You're showing screenshots from different programs/terminals, some of which may be configured to quietly handle windows-style line endings. Use something like `od -c file.php` to check the file before and after the substitution. – Tom Fenech Jun 16 '16 at 09:19
  • What is the file line ending ? Is it CRLF (Windows) ? I already had this issue on windows created files when editing them on linux, it added ^M on each updated lines. If this is the case, you need to change the file line ending – micster Jun 16 '16 at 09:23
  • Something else is going on here. I'm 99% certain, this is nothing to do with sed. – Jonnix Jun 16 '16 at 09:23
  • The file is original file from wordpress https://wordpress.org/latest.zip and name of it is wp-config-sample.php http://prntscr.com/bh140p – mirza Jun 16 '16 at 09:25
  • Rather than posting all of these screenshots in the comments, just run `od -c wp-config.php` before and after the substitution and [edit] your question to show the output for the relevant lines. – Tom Fenech Jun 16 '16 at 09:27
  • 1
    Ok it must be this : wp-config-sample has CRLF line ending – micster Jun 16 '16 at 09:27
  • You don't need `s` command for `sed`. You can use `"/'WP_DEBUG', false);/a define('WP_MEMORY_LIMIT', '96M');\r"` – anishsane Jun 16 '16 at 11:34

3 Answers3

18

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.

anishsane
  • 20,270
  • 5
  • 40
  • 73
  • Great explanation, It helped me to solve the issue thank you. Please check the note that I added :) – mirza Jun 16 '16 at 11:11
1

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/

micster
  • 758
  • 3
  • 10
  • `^M` is actually a single character, @micster. It is the line-feed character (`\r` in `C` language and friends). – anishsane May 08 '17 at 05:01
  • `some_command filename > filename` is almost never what you want. The redirection operator `>` will truncate the file before the `some_command` start processing it. Either use `some_command filename > filename.new && mv filename.new filename` or use an option like `--in-place` if your command supports it. (`sed` does.) – anishsane Apr 02 '19 at 04:28
-1

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.

Jonnix
  • 4,121
  • 1
  • 30
  • 31
  • I think @micster has a point. The file from https://wordpress.org/latest.zip here has windows CRLF file endings. – mirza Jun 16 '16 at 09:39
  • and if you try number 1 it will be like in the questions output. – mirza Jun 16 '16 at 09:59