viまたは他のツールを使ってパターンの後の単語/数字をどのように変更できますか?ただし、単語だけを変え、続く内容は変えないでください。

viまたは他のツールを使ってパターンの後の単語/数字をどのように変更できますか?ただし、単語だけを変え、続く内容は変えないでください。

複数のスクリプトでポート番号を0に変更したいのですが、ポート0の後のテキストは同じままにしたいと思います。これを行う方法はありますか? viを介してモードを変更できますが、ポート番号は両方とも一意であるため変更できません。ありがとうございます!

local-ip 159.105.100.40 port 5510 remote-ip 152.16.142.104 port 3868 

ベストアンサー1

Whisはsed簡単です。

$ foo="local-ip 159.105.100.40 port 5510 remote-ip 152.16.142.104 port 3868"
$ echo "$foo" | sed 's/port [0-9]\{1,5\}/port 0/g'
local-ip 159.105.100.40 port 0 remote-ip 152.16.142.104 port 0

だから

# let's suppose that all your scripts are in the same directory
# and have the extension .sh
for file in *.sh; do
  # WARNING: the -i option writes the file
  # so it's better to try first without it
  sed -i 's/port [0-9]\{1,5\}/port 0/g' "$file"
done


vi同じコマンドを使用できます。

:s/port [0-9]\{1,5\}/port 0/g

それとももっと簡単です。@クワジモド提案:

 :s/port \d\+/port 0/g

おすすめ記事