[英]Find/Replace various lines in file with find/sed via bash
I use the below command to find/replace text in files of the name indicated within the path shown.我使用以下命令在显示的路径中指示的名称的文件中查找/替换文本。
I need to use a similar command for a number of lines in a different file of which there are over 1,000 so I can't so them all individually.我需要对不同文件中的多行使用类似的命令,其中有超过 1,000 行,所以我不能单独使用它们。
find /var/www/html/files/ -name "file.php" -print | xargs sed -i 's/Find Text/Replace Text/g'
The above finds files in the location /var/www/html/files/
(and folders within) and finds the text 'Find Text' and replaces it with 'Replace Text'.上面的代码在
/var/www/html/files/
(以及其中的文件夹)中查找文件,并找到文本“查找文本”并将其替换为“替换文本”。
How could I use this if I wanted to find a whole line within the php files and replace it with three new lines of code?如果我想在 php 文件中找到一整行并用三行新代码替换它,我该如何使用它?
Alternatives I could look at are to add the lines of code at the end of each file OR to simply copy a whole new file to each location and overwrite the previous one.我可以考虑的替代方法是在每个文件的末尾添加代码行,或者简单地将一个全新的文件复制到每个位置并覆盖前一个。
Thanks.谢谢。
Lets say this is my file :假设这是我的文件:
$ cat change_version.py
#!/usr/local/bin/python3
import json
obj=json.load(open('input.json'))
obj['version'] = "WHATEVER"
print(json.dumps(obj, indent=" ")) . <-- I want to replace this line
Then this will do :然后这样做:
cat change_version.py | sed $'s/print.*/morethan\\\noneline/g'
#!/usr/local/bin/python3
import json
obj=json.load(open('input.json'))
obj['version'] = "WHATEVER"
morethan
oneline
Let me know if it works for you.请让我知道这对你有没有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.