sed使用心得

来源:互联网 发布:映客 h5 源码 编辑:程序博客网 时间:2024/06/05 03:17

原始文件内容

A
BC
eee
BluefinAsserts.verifyTrue(true, "25899");
BluefinAsserts.verifyTrue(true, "258xY399b");
BluefinAsserts.verifyTrue(isFound, "verify xxx");
BluefinAsserts.assertTrue(isFound, "Verify " + msg);
sss



需求:移除 BluefinAsserts.verifyTrue(true, "xxxxxx");

由于存在若干BluefinAsserts.verifyTrue开头的验证方法,所以,需要小心构建需要移除内容的正则表达式


最终使用的是

sed -i "s/BluefinAsserts\.verifyTrue(true, \"[[:alnum:]]\{3,20\}\");$//g" `grep -E "BluefinAsserts\.verifyTrue\(true.*\);$" -rl ./`


grep 需要用-E显示声明使用正则表达式来查找内容

-l用来列出文件名


sed -i 用来直接修改文件内容

值得注意的是:

1, sed中的括号不需要转义

2. [[:alpha:]]是匹配字母的,[:alpha:]是匹配中括号里面的任意一个字母

3. 如果需要用到{m,n}来控制数量,需要使用\{m,n\}!

PS: 在git bash下测试所得结果


参考资料:

1. SED可以使用的正则表达式

http://www.sunnyu.com/?p=130

附:sed新手使用进阶全功略

http://www.linuxsir.org/bbs/thread189620.html

2. sed-removing-alphanumeric-words-from-a-file

http://stackoverflow.com/questions/4433201/sed-removing-alphanumeric-words-from-a-file

3. sed 中小括号比如(如何转义

http://bbs.chinaunix.net/thread-1410050-1-1.html

4. sed常用正则表达式

http://www.360doc.com/content/09/0420/16/36491_3201565.shtml

5. sed + 正则详细说明(中文)

http://blog.csdn.net/cxqdong/article/details/2007884

6. 谁能解释下[:alpha:]和[[:alpha:]]的区别
http://bbs.linuxtone.org/thread-9103-1-1.html

7. sed example

http://www.theunixschool.com/2012/06/sed-25-examples-to-delete-line-or.html




扩展

1. efficient-in-place-header-removing-for-large-files-using-sed

http://unix.stackexchange.com/questions/25629/efficient-in-place-header-removing-for-large-files-using-sed

2. gnu - sed

http://www.gnu.org/software/sed/manual/sed.html

3. sed FAQ

http://sed.sourceforge.net/sedfaq.html


原创粉丝点击