Skip to main content
Improved answer.
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54

Try this test:

sed 's/^    "peer-port": [0-9]*,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/^    "peer-port": [0-9]*,/    "peer-port": '"${newport}"',/' settings.json

Notes: the substitution strings may need a little tweaking, (i.e. replacing " " with "\t"), depending on whether the whitespace is spaces or tabs.

A more compact and efficient, (but maybe less portable), method:

sed `/^    "peer-port": /{s/ [0-9]*,/ '"${newport}",'/;q}' settings.json

Try this test:

sed 's/    "peer-port": [0-9]*,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/    "peer-port": [0-9]*,/    "peer-port": '"${newport}"',/' settings.json

Notes: the substitution strings may need a little tweaking, (i.e. replacing " " with "\t"), depending on whether the whitespace is spaces or tabs.

A more compact and efficient, (but maybe less portable), method:

sed `/    "peer-port": /{s/ [0-9]*,/ '"${newport}",'/;q}' settings.json

Try this test:

sed 's/^    "peer-port": [0-9]*,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/^    "peer-port": [0-9]*,/    "peer-port": '"${newport}"',/' settings.json

Notes: the substitution strings may need a little tweaking, (i.e. replacing " " with "\t"), depending on whether the whitespace is spaces or tabs.

A more compact and efficient, (but maybe less portable), method:

sed `/^    "peer-port": /{s/ [0-9]*,/ '"${newport}",'/;q}' settings.json
Improved answer, as per OP spec.
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54

Try this test:

sed 's/    "peer-port": 23456[0-9]*,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/    "peer-port": 23456[0-9]*,/    "peer-port": '"${newport}"',/' settings.json

NoteNotes: the substitution strings may need a little tweaking, (i.e. replacing " " with "\t"), depending on whether the whitespace is spaces or tabs.

A more compact and efficient, (but maybe less portable), method:

sed `/    "peer-port": /{s/ [0-9]*,/ '"${newport}",'/;q}' settings.json

Try this test:

sed 's/    "peer-port": 23456,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/    "peer-port": 23456,/    "peer-port": '"${newport}"',/' settings.json

Note: the substitution strings may need a little tweaking, (i.e. replacing " " with "\t"), depending on whether the whitespace is spaces or tabs.

Try this test:

sed 's/    "peer-port": [0-9]*,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/    "peer-port": [0-9]*,/    "peer-port": '"${newport}"',/' settings.json

Notes: the substitution strings may need a little tweaking, (i.e. replacing " " with "\t"), depending on whether the whitespace is spaces or tabs.

A more compact and efficient, (but maybe less portable), method:

sed `/    "peer-port": /{s/ [0-9]*,/ '"${newport}",'/;q}' settings.json
Improved text.
Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54

Try this test:

sed 's/    "peer-port": 23456,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/    "peer-port": 23456,/    "peer-port": '"${newport}"',/' settings.json

Note: the substitution strings may need a little tweaking, (i.e. replacing " " with "\t"), depending on whether the whitespace is spaces or tabs.

Try this test:

sed 's/    "peer-port": 23456,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/    "peer-port": 23456,/    "peer-port": '"${newport}"',/' settings.json

Note: the substitution strings may need a little tweaking, depending on whether the whitespace is spaces or tabs.

Try this test:

sed 's/    "peer-port": 23456,/    "peer-port": '"${newport}"',/' settings.json

...examine the output, and if it looks good, add an -i to actually change the file like so:

sed -i 's/    "peer-port": 23456,/    "peer-port": '"${newport}"',/' settings.json

Note: the substitution strings may need a little tweaking, (i.e. replacing " " with "\t"), depending on whether the whitespace is spaces or tabs.

Source Link
agc
  • 7.4k
  • 4
  • 25
  • 54
Loading