简体   繁体   中英

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.

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'.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM