shell 之 使用sed替换文本中某个字符串

发布时间 2023-03-30 16:22:02作者: by1314
#!/bin/bash

if [ $# -lt 3 ]
then
  echo "Usage:$0 <old_string> <new_string> <file path>"
  exit 1
fi

old_string=$1
new_string=$2
file_path=$3

if [ -f "$file_path" ]
then
  sed -i "s|$old_string|$new_string|g" $file_path
  echo "string $old_string has been repalced with $new_string in file $file_path"
else
  echo " File $file_path does not exist"
  exit 1
fi