Linux 用于替换自定义目录中整行代码的可执行脚本

Linux 用于替换自定义目录中整行代码的可执行脚本,linux,bash,sed,command-line,text-processing,Linux,Bash,Sed,Command Line,Text Processing,有没有办法通过脚本替换一行js代码?。也许和塞德在一起。。。我想用另一个url替换Ajax函数的url属性。我想通过脚本更改的代码行位于路径中: 网站项目/src/client-side/js/custom scripts.js我想更改的全部内容是: url: "https://us-central1-develop-website.cloudfunctions.net/send_contact", to url: "https://us-central1-pr

有没有办法通过脚本替换一行js代码?。也许和塞德在一起。。。我想用另一个url替换Ajax函数的url属性。我想通过脚本更改的代码行位于路径中: 网站项目/src/client-side/js/custom scripts.js我想更改的全部内容是:

url: "https://us-central1-develop-website.cloudfunctions.net/send_contact",

to

url: "https://us-central1-production-website-prd.cloudfunctions.net/send_contact"

使用sed,您可以这样做:

sed -i 's/url: "https:\/\/us-central1-develop-website.cloudfunctions.net\/send_contact"/url: "https:\/\/us-central1-production-website-prd.cloudfunctions.net\/send_contact"/' website-project/src/client-side/js/custom-scripts.js
为了使脚本更具可读性,您可以借助变量对其进行分解:

#/bin/bash
从url:“https://us-central1-develop-website.cloudfunctions.net/send_contact"'
到url:“https://us-central1-production-website-prd.cloudfunctions.net/send_contact"'
target_file=website project/src/client-side/js/custom-scripts.js
sed-i“$(printf的+%s+%s+”$从“$到“$目标文件”

使用sed,您可以这样做:

sed -i 's/url: "https:\/\/us-central1-develop-website.cloudfunctions.net\/send_contact"/url: "https:\/\/us-central1-production-website-prd.cloudfunctions.net\/send_contact"/' website-project/src/client-side/js/custom-scripts.js
为了使脚本更具可读性,您可以借助变量对其进行分解:

#/bin/bash
从url:“https://us-central1-develop-website.cloudfunctions.net/send_contact"'
到url:“https://us-central1-production-website-prd.cloudfunctions.net/send_contact"'
target_file=website project/src/client-side/js/custom-scripts.js
sed-i“$(printf的+%s+%s+”$从“$到“$目标文件”

除非我弄错了,否则此Ed脚本应该可以实现以下功能:-

#!/bin/sh

cat >> edrep.txt << EOF

/develop-website/    # This command will get you to the line with this string.
s/develop-website/production-website-prd/    # This will modify the line for you.
wq    # This writes to the file and quits.

EOF

ed -s website-project/src/client-side/js/custom-scripts.js < edrep.txt
rm -v ./edrep.txt
#/垃圾箱/垃圾箱

cat>>edrep.txt除非我弄错了,否则此Ed脚本应该可以实现以下功能:-

#!/bin/sh

cat >> edrep.txt << EOF

/develop-website/    # This command will get you to the line with this string.
s/develop-website/production-website-prd/    # This will modify the line for you.
wq    # This writes to the file and quits.

EOF

ed -s website-project/src/client-side/js/custom-scripts.js < edrep.txt
rm -v ./edrep.txt
#/垃圾箱/垃圾箱

猫>>edrep.txt嘿!感谢您的帮助,它不起作用,它运行了:sed:无法读取网站项目/src/client-side/js/custom-scripts.js没有这样的文件或目录您必须将
网站项目/src/client-side/js/custom scripts.js
替换为文件的完整路径,类似于
/var/www/website-project/../custom scripts.js
,或者
~/website-project/../custom-scripts.js
或者其他任何东西,取决于js文件的位置!我走错了路。成功了!谢谢你的帮助!嘿感谢您的帮助,它不起作用,它运行了:sed:无法读取网站项目/src/client-side/js/custom-scripts.js没有这样的文件或目录您必须将
网站项目/src/client-side/js/custom scripts.js
替换为文件的完整路径,类似于
/var/www/website-project/../custom scripts.js
,或者
~/website-project/../custom-scripts.js
或者其他任何东西,取决于js文件的位置!我走错了路。成功了!谢谢你的帮助!