sed 替换命令

发布时间 2023-10-11 18:46:07作者: profesor

Create a Backup
To create a backup file before overwriting the existing one, add the .bak parameter to the -i tag.

sed -i.bak 's/foo/FOO/g' example.txt

 

Match and Replace All Cases
To find and replace all instances of a word and ignore capitalization, use the I parameter:

sed -i 's/bar/linux/gI' example.txt


Reference Found String
Use the ampersand character (&) to reference the found string. For example, to add a forward slash (/) before every instance of foo in a file, use:

sed -i 's/foo/\/&/gI'example.txt


Recursive Find and Replace
Use the find command to search for files and combine it with sed to replace strings in files recursively. For example:

find -name 'example*' -exec sed -i 's/[a-z]/5/gI' {} +

 

 

来源:https://phoenixnap.com/kb/sed-replace