Common Commands for Sed on FreeBSD
Replace string:
% sed -i .bak 's/original/new/g' test.txt
-i Edit files in-place, saving backups with the specified extension(here it is .bak).
Replace the comments of C language:
% sed 's/\(\/\*\).*\(\*\/\)//g' test.c
Delete the line contains pattern string:
% sed -i .bak '/pattern_string/d' file.txt
Add new line after pattern string:
% sed -E 's/(pattern_string)/\1\nnew line/' file.txt
-E Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's).
\1 Represent pattern string.
Escape single quote:
% sed 's/ones/one\x27s/'
And string ones thing will become one's thing.
Copy specific lines in a file to another file:
copy lines 10 to 15 of file1.txt into file2.txt.
% sed -n '10,15p' file1.txt > file2.txt
Non-greedy matching for ssed (super sed).
The content of file test.txt was as follows:
ssABteAstACoABnnACss
Non-greed matching command as follows:
% ssed -R 's/AB.*?AC/XXX/g' test.txt
Operation on file test.txt directly(NO BACKUP):
% ssed -i -R 's/AB.*?AC/XXX/g' test.txt
Then,the content of filte test.txt is as follows:
ssXXXoXXXss
There is another tricky way: Non-greedy match with SED regex (emulate perl's .*?)
Comments
Post a Comment