FreeBSD/Linux 下各种查找替换字符串的办法

在Freebsd下我们常常要进行字符串的查找替换,有在vim编辑器里面进行的,也有在shell下进行的,这样的事情常遇到,但是每次都会感到有些概念模糊,尤其对类似awk、sed、grep、find等命令的具体参数使用问题上老是糊涂,下面收集几个常用的方法,实在不能理解就死记算了!

1、在vi中使用的查找替换方法
利用 :s 命令可以实现字符串的替换。具体的用法包括:
:s/str1/str2/ 用字符串 str2 替换行中首次出现的字符串 str1
:s/str1/str2/g 用字符串 str2 替换行中所有出现的字符串 str1
:.,$ s/str1/str2/g 用字符串 str2 替换正文当前行到末尾所有出现的字符串 str1
:1,$ s/str1/str2/g 用字符串 str2 替换正文中所有出现的字符串 str1
:g/str1/s//str2/g 功能同上从上述替换命令可以看到:g 放在命令末尾,表示对搜索字符串的每次出现进行替换;不加 g,表示只对搜索字符串的首次出现进行替换;g 放在命令开头,表示对正文中所有包含搜索字符串的行进行替换操作。

2、在shell中使用find结合grep进行文件的替换
# find ./ -exec grep str1 ‘{}’ \; -exec sed -i.bak s/str1/str2/g ‘{}’ \;
上面命令可以在当前目录下(包括子目录)查找包含str1的文件,自动替换成str2,并且生成源文件的bak文件

更多方法正在不断收集更新中。。。

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
One Response
  1. dyfire says:

    很实用的东西~:)  谢谢 大哥
    是否可以转载过去

  2. minecraft says:

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.

    I think that you could do with some pics to drive the message home a little bit, but instead of
    that, this is excellent blog. A great read. I will definitely be back.

  3. minecraft says:

    Hey are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and set
    up my own. Do you require any coding expertise to make your own blog?
    Any help would be really appreciated!

  4. Wonderful article! This is the kind of info that should be shared around the net.
    Shame on Google for not positioning this post higher!
    Come on over and discuss with my site . Thank you =)

  5. When I originally left a comment I appear to have clicked the -Notify me
    when new comments are added- checkbox and from now on every time a comment is added I receive 4 emails with the exact
    same comment. Is there a means you can remove me from that service?
    Many thanks!

  6. Hi there I am so happy I found your site, I really found you
    by mistake, while I was researching on Digg for something
    else, Nonetheless I am here now and would just like to say many thanks for a remarkable
    post and a all round exciting blog (I also love the theme/design), I don’t have time to read through it all at the moment but I have saved it and also added
    in your RSS feeds, so when I have time I will be back to
    read much more, Please do keep up the superb jo.

  7. Howdy just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Opera.

    I’m not sure if this is a format issue or something to
    do with browser compatibility but I figured I’d post to let you know.
    The style and design look great though! Hope you get the issue
    resolved soon. Kudos

  8. Coconut Oil says:

    After I initially left a comment I seem to have clicked
    the -Notify me when new comments are added- checkbox and from now on each time a comment
    is added I receive four emails with the exact same comment.
    Perhaps there is a means you are able to remove me from that service?

    Thank you!

  9. Hi would you mind letting me know which web host you’re working
    with? I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot faster
    then most. Can you suggest a good hosting provider at a fair price?

    Cheers, I appreciate it!

  10. I’m curious to find out what blog platform you’re working with?
    I’m having some small security issues with my latest site
    and I’d like to find something more safe.
    Do you have any recommendations?

  11. Greetings! Very helpful advice within this post! It
    is the little changes that will make the largest changes.
    Thanks for sharing!

  12. It’s amazing in support of me to have a web page, which is beneficial in favor of my
    experience. thanks admin

  13. Sling TV says:

    My spouse and I stumbled over here from a different
    page and thought I might check things out. I like what I see so now i am following you.

    Look forward to checking out your web page for a second time.

  14. Simply want to say your article is as amazing.
    The clearness for your publish is simply excellent
    and i could think you’re a professional on this subject.
    Well along with your permission let me to seize your RSS feed to
    keep updated with impending post. Thanks 1,000,000 and please continue the rewarding work.

  15. Hi there friends, its fantastic post regarding educationand completely explained, keep it up all the time.

  16. Hello would you mind stating which blog platform you’re working with?
    I’m looking to start my own blog soon but I’m having a tough time
    making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique.
    P.S Sorry for being off-topic but I had to ask!

  17. Keep on writing, great job!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image