Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • I actually forgot to mention that the port will never be the same. The sed command needs to replace the old port (random 5 digit port) with one stored in $newport Commented Apr 21, 2019 at 0:20
  • @Shr00m, See revised answer. Commented Apr 21, 2019 at 0:23
  • @agc instead of trying to guess what the leading whitespace is composed of (and the exact number of spaces or tabs) you should be able to use \s*? Commented Apr 21, 2019 at 0:38
  • 1
    Further suggestion to improve upon @agc answer, this is a little more concise: sed -i -E 's/^(\s*?"peer-port":\ )[0-9]{5},/\1'"$newport"'/' settings.json Commented Apr 21, 2019 at 0:50
  • @JeffH., Well maybe, but this is a novice question and \s is both harder to explain and not always intuitive. Consider the output of this code echo foo | sed 's/\s*/\t/'; there's no visible leading whitespace in the input, but there is in the output... Also it's uncertain whether or not the OP is running GNU sed; if not then stuff like [0-9]{5} won't work. The lead ^ is an improvement in any case though. Commented Apr 21, 2019 at 0:52