-1

I need to replace -Xmx1024m (changing 1024 to 2048) in standalone.conf from a shell command.

I try do this with sed:

echo $(sed 's/1024/2048/g' standalone.conf) > standalone.conf.

The code works, but the text saved loses tabs and newlines.

  • 2
    The given code will not have that effect. Presumably you're doing something else as well (probably involving loading content into a variable and using `echo $varname` to print it). – Charles Duffy Feb 18 '16 at 20:15
  • 1
    ...in general, when asking a question, it's useful to test that you've included enough code to precisely reproduce the problem at hand. – Charles Duffy Feb 18 '16 at 20:16
  • sorry, now i understand... its because echo – Diogo Maschio Feb 18 '16 at 20:25
  • without echo the code works correctly... only sed 's/1024/2048/g' standalone.conf > standalone.conf – Diogo Maschio Feb 18 '16 at 20:28
  • 2
    That's actually not reliable, with the same filename for both input and output; it can overwrite the output file *before* reading the original content. – Charles Duffy Feb 18 '16 at 20:33
  • Anyhow, that clarification makes this a duplicate of http://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else – Charles Duffy Feb 18 '16 at 20:34
  • 2
    ...and btw, not so much the echo itself, but the *unquoted* expansion used as your argument to echo; `echo "$(...)"` wouldn't have this same effect, though there would still be other bugs. – Charles Duffy Feb 18 '16 at 20:37

3 Answers3

5

Passing an expansion to echo unquoted subjects it to string-splitting and glob-expansion, passing every individual word produced by those processes as a separate argument to echo (which echo then combines with a single space between each).

Consider instead:

sed 's/1024/2048/g' <standalone.conf >standalone.conf.new && mv standalone.conf{.new,}

...and, in general, always use echo "$foo" or instead of echo $foo -- it's the lack of quotes here which was most immediately responsible for your bug.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1

Do not use echo, but let sed change the file:

sed -i 's/1024/2048/g' standalone.conf
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • This isn't portable: `-i` isn't specified by POSIX at all, and moreover, many BSD implementations make the extension argument to `-i` mandatory, meaning that many versions will fail with this usage *even if they **do** support `-i`*. – Charles Duffy Feb 19 '16 at 14:34
1

sed 's/1024/2048/g' will translate 1024 to 2048 globally, throughout the file, which might be unwise. It would be much better to limit the translation somehow. If your goal is in fact the one you stated, namely changing -Xmx1024m to -Xmx2048m, then the following would at least be a reasonable start (assuming your sed supports the -i option):

sed -i -e 's/-Xmx1024m/-Xmx2048m/' standalone.conf

(If your sed does not support -i, then make the obvious changes.)

If the timestamp of the file is useful, and if you only want to update the file if it needs updating, then consider:

grep -q -e -Xmx1024m standalone.conf && sed -i -e 's/-Xmx1024m/-Xmx2048m/' standalone.conf
peak
  • 105,803
  • 17
  • 152
  • 177
  • As I've indicated to Walter, `sed -i` isn't portable; see the POSIX sed spec at http://pubs.opengroup.org/onlinepubs/007904975/utilities/sed.html; even on platforms that *do* support `sed -i`, the exact usage you gave here can fail -- try it on OS X. POSIX *does* provide editors which are guaranteed to support in-place editing functionality; both `ed` and `ex` are in this category, but `sed` is a *stream* editor, not a *file* editor. – Charles Duffy Feb 19 '16 at 14:35
  • @charlesduffy - I said "reasonable start" mainly because of the -i issue, but have clarified. I've added -e to make sure it works with OS X (BSD). Thanks. – peak Feb 19 '16 at 17:15