sed: -e表达式#1,char 7:未终止的' s'命令。

时间:2022-10-14 20:13:20
port.properties
{
  "port":3006
  "name":"John"
}

changeport.sh

changeport.sh

filename="port.properties"
oldport=$(awk -F'"port":|,' '{i=$2;print i}'  "$filename")
read newport
oldport_str="\"port\":$oldport"
newport_str="\"port\":$newport"

sed -i "s/$oldport_str/$newport_str/g" "$filename"

I don't know about the error.If you have the answer,please tell me,thanks

我不知道这个错误。如果你有答案,请告诉我,谢谢。

2 个解决方案

#1


1  

Something in the old value is off; it contains an invisible character (DOS carriage return?) or something you are not showing us.

旧价值里的东西消失了;它包含一个看不见的字符(DOS回车)或者你没有显示给我们的东西。

Anyway, finding the old value using a regex just so you can replace it is not preventing any accidents here, so the simplest fix is to remove the part which reads the old value.

无论如何,使用正则表达式找到旧值,这样你就可以替换它,这并不能防止任何事故发生,所以最简单的解决方法是删除读取旧值的部分。

read newport
sed -i "s/\(\"port\":\).*/\1$newport/" port.properties

Some sed dialects do not require backslashes before capturing parentheses; experiment with the precise syntax for your version before committing anything.

一些sed方言在捕捉圆括号之前不需要反斜线;在提交任何东西之前,要对您的版本进行精确的语法实验。

#2


1  

just change the whole thing to a simple:

只是把整个事情变成一个简单的:

filename="port.properties"
read -r newport
awk -v new="$newport" 'BEGIN{FS=OFS=":"} $1~/"port"/{$2=new} 1' "$filename" > tmp &&
mv tmp "$filename"

The above will work no matter what characters are in your file or read input string.

无论您的文件中有什么字符或读取输入字符串,上述操作都是有效的。

#1


1  

Something in the old value is off; it contains an invisible character (DOS carriage return?) or something you are not showing us.

旧价值里的东西消失了;它包含一个看不见的字符(DOS回车)或者你没有显示给我们的东西。

Anyway, finding the old value using a regex just so you can replace it is not preventing any accidents here, so the simplest fix is to remove the part which reads the old value.

无论如何,使用正则表达式找到旧值,这样你就可以替换它,这并不能防止任何事故发生,所以最简单的解决方法是删除读取旧值的部分。

read newport
sed -i "s/\(\"port\":\).*/\1$newport/" port.properties

Some sed dialects do not require backslashes before capturing parentheses; experiment with the precise syntax for your version before committing anything.

一些sed方言在捕捉圆括号之前不需要反斜线;在提交任何东西之前,要对您的版本进行精确的语法实验。

#2


1  

just change the whole thing to a simple:

只是把整个事情变成一个简单的:

filename="port.properties"
read -r newport
awk -v new="$newport" 'BEGIN{FS=OFS=":"} $1~/"port"/{$2=new} 1' "$filename" > tmp &&
mv tmp "$filename"

The above will work no matter what characters are in your file or read input string.

无论您的文件中有什么字符或读取输入字符串,上述操作都是有效的。