Batch replace and delete specified strings in multiple files


1. Linux sed batch replace strings in multiple files

sed -i "s/oldstring/newstring/g" `grep oldstring -rl datadir`

For example: replace testString in all files under /data with newString

sed -i "s /testString/newString/g" `grep testString -rl /data`


Second, batch delete the lines of the specified string

sed -e '/test/d' test.txt // Delete the lines containing "test" in test.txt, But do not change the test.txt file itself, the result after the operation is displayed in the terminal

sed -e '/test/d' test.txt > test_new.txt // Delete the line containing "test" in test.txt, and put the line after the operation save the result to test_new.txt

sed '/test/d;/boy/d' test.txt > test_new.txt // delete the line containing the string "test" or "boy" and save the result to test_new.txt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326225489&siteId=291194637