Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何使用sed查找并替换为特殊字符?_Macos_Sed - Fatal编程技术网

Macos 如何使用sed查找并替换为特殊字符?

Macos 如何使用sed查找并替换为特殊字符?,macos,sed,Macos,Sed,我想用$(cat apiServer.txt)替换ip'100.45.9.147'。 非就地更新有效,但当我尝试使用sed的就地更新时,我得到错误:- $sed“s/'100.45.9.147'/\$(cat apiServer.txt)/g“checkNTP.sh echo -e "\n" apiServer=$(cat apiServer.txt) usertoken=$(cat tokenfile.txt) curl -k -H "Authorization: Bearer ${user

我想用
$(cat apiServer.txt)
替换ip
'100.45.9.147'
。 非就地更新有效,但当我尝试使用sed的就地更新时,我得到错误:-

$sed“s/'100.45.9.147'/\$(cat apiServer.txt)/g“checkNTP.sh

echo -e "\n"
apiServer=$(cat apiServer.txt)
usertoken=$(cat tokenfile.txt)
curl -k  -H "Authorization: Bearer ${usertoken}" -H 'Content-Type: application/json' "https://${apiServer}/configapi/v1/SystemSettingsNTP"

echo -e "\n\n"
sed: 1: "checkNTP.sh": command c expects \ followed by text
$sed-i“s/'100.45.9.147'/\$(cat apiServer.txt)/g“checkNTP.sh

echo -e "\n"
apiServer=$(cat apiServer.txt)
usertoken=$(cat tokenfile.txt)
curl -k  -H "Authorization: Bearer ${usertoken}" -H 'Content-Type: application/json' "https://${apiServer}/configapi/v1/SystemSettingsNTP"

echo -e "\n\n"
sed: 1: "checkNTP.sh": command c expects \ followed by text
下面是checkNTP.sh的内容

echo -e "\n"
apiServer='100.45.9.147'
usertoken=$(cat tokenfile.txt)
curl -k  -H "Authorization: Bearer ${usertoken}" -H 'Content-Type: application/json' "https://${apiServer}/configapi/SystemSettingsNTP"

echo -e "\n\n"
使用下面的
-i'
开关解决了这个问题:

sed-i''s/'10.45.9.147'/\\\\$\(cat apiServer.txt\\)/g“fileName.txt

这可能适合您(GNU-sed):

单引号最好从shell中插入引号,这允许元字符以外的所有其他字符表示它们自己,并且元字符可以作为正常字符引用,例如
必须被引用
\。
否则它只表示单个字符

如果希望shell变量再次替换文本,请在shell中戳一个孔,并用双引号将变量括起来,以便留出可能的空白。还请记住,如果变量包含元字符,则应事先引用这些元字符,否则替换命令可能会导致sed语法错误。这通常发生在变量中的字符与替换分隔符冲突时,即通常
/