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


利用 :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 放在命令开头,表示对正文中所有包含搜索字符串的行进行替换操作。

# find ./ -exec grep str1 ‘{}’ \; -exec sed -i.bak s/str1/str2/g ‘{}’ \;


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.

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