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!

  18. These are truly enormous ideas in about blogging.
    You have touched some good points here. Any way keep up wrinting.

  19. Pingback: URL
  20. Download says:

    There’s certainly a great deal to know about this issue.
    I really like all of the points you have made.

  21. You’ve made some good points there. I looked on the internet
    to learn more about the issue and found most individuals will go along with your views on this
    web site.

  22. post-141065 says:

    of course like your website but you have to check the spelling on several of your posts.
    Several of them are rife with spelling problems and I in finding it very
    bothersome to tell the truth on the other hand I will surely come back again.

  23. download says:

    Simply desire to say your article is as astounding.

    The clarity on your put up is just great and that i can think you’re
    an expert in this subject. Well with your permission allow me to
    grasp your feed to keep updated with impending post.
    Thanks one million and please carry on the enjoyable work.

  24. download says:

    I could not resist commenting. Very well written!

  25. read me says:

    Aw, this was a very nice post. Taking a few minutes and actual effort to generate a good article… but what can I say… I hesitate a
    whole lot and never manage to get anything done.

  26. minecraft says:

    Thank you for the good writeup. It in fact was a amusement
    account it. Look advanced to far added agreeable from
    you! However, how can we communicate?

  27. tinyurl.com says:

    This is my first time pay a visit at here and i am actually pleassant to read
    all at one place.

  28. minecraft says:

    I like what you guys are usually up too. This type of clever work and exposure!
    Keep up the terrific works guys I’ve you guys to blogroll.

  29. 7 says:

    Hello, I want to subscribe for this blog to obtain newest updates, thus where can i do
    it please help out.

  30. gamefly says:

    I simply couldn’t depart your web site before suggesting that I really enjoyed the usual info
    a person supply for your visitors? Is going to be back incessantly
    in order to investigate cross-check new posts

  31. gamefly says:

    I pay a quick visit everyday some web sites and information sites to read articles or reviews, except this webpage gives quality based articles.

  32. Great post. I was checking constantly this blog and I am impressed!
    Very helpful information particularly the last part 🙂
    I care for such info much. I was looking for this certain information for a very long time.

    Thank you and best of luck.

  33. Ahaa, its fastidious conversation about this piece of writing here at this web site, I have read all that, so
    now me also commenting here.

  34. Thanks in favor of sharing such a nice thinking, piece of writing is pleasant,
    thats why i have read it fully

  35. g says:

    I was able to find good information from your blog articles.

  36. g says:

    Hello There. I discovered your weblog using msn. This is a very smartly written article.
    I’ll be sure to bookmark it and return to read extra of your helpful
    info. Thank you for the post. I will definitely return.

  37. g says:

    Woah! I’m really loving the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between user friendliness
    and appearance. I must say that you’ve done a fantastic job with
    this. In addition, the blog loads super quick for me on Opera.
    Excellent Blog!

  38. g says:

    Aw, this was a very nice post. Taking the time and actual effort to create a great article… but what can I say… I
    put things off a lot and don’t manage to get nearly anything done.

  39. g says:

    If you are going for finest contents like me, simply go to
    see this web page all the time since it presents feature contents, thanks

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