I have file named 'server.cfg'. There is string 'port 7788', 7788 can be any number ( 7799, 7711 and so on ), i need to change that number which goes after port to 7777, how can i do that? I need ssh command, using debian. Thanks.
Asked
Active
Viewed 107 times
-3
-
Try `man sed` and go through some examples. You'll find it pretty easy. – jaypal singh Jul 10 '14 at 13:52
-
possible duplicate of [How do I use sed to change my configuration files, with flexible keys and values?](http://stackoverflow.com/questions/5955548/how-do-i-use-sed-to-change-my-configuration-files-with-flexible-keys-and-values) – mc110 Jul 19 '14 at 06:46
3 Answers
1
You can use sed
, like this:
sed -i 's/port \([0-9]*\)/port 7777/' server.cfg

hek2mgl
- 152,036
- 28
- 249
- 266
0
Sounds like you need sed. If you want to change one specific instance use:
$ sed -i 's/port 7788/port 7777/g' server.cfg
If you want to change an unknown number multiple times use:
$ sed -i 's/port \([0-9]*\)/port 7777/' server.cfg
If you want to change only the first instance found, use:
$ sed -i '0,/port \([0-9]*\)/{s/port \([0-9]*\)/port 7777/}' server.cfg

wbt11a
- 798
- 4
- 12
-
ok, but as i have said, port XXXX, so i don't know what number is after port. – user3822243 Jul 10 '14 at 13:54
-
-
0
You should use a regegex with sed :
sed -i 's/port [[:digit:]]\{4\}/port 7777/g' server.cfg
This will replace all the port number (with 4digits) by 7777

Fabien
- 194
- 1
- 10
-
1
-
You can do like this : sed -i 's/port [[:digit:]]\{1,5\}/port 7777/g' server.cfg – Fabien Jul 10 '14 at 14:42