Results 1 to 3 of 3

Thread: Replacing an entire line based on finding part of it

  1. #1
    Join Date
    Dec 2012
    Beans
    74

    Replacing an entire line based on finding part of it

    In a shell script i can directly replace text using:

    sed -i "s/texta/textb/g" /home/me/myfile

    I there any way i can replace an entire line, by only looking for part of it?

    ie, say myfile contained
    "the quick red fox"
    "the quick brown fox"
    "the quick blue fox"

    and i wanted to replace the line with brown in its entirety for "the lazy dog" - the only bit i could search for would be a line containing "brown" but i also want to replace "the quick" and "fox"

    any help gratefully appreciated.

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Replacing an entire line based on finding part of it

    Sure
    Code:
    $ for color in red brown blue;do echo the quick $color fox;done
    the quick red fox
    the quick brown fox
    the quick blue fox
    Code:
    $ for color in red brown blue;do echo the quick $color fox;done | sed '/brown/s/.*/the lazy dog/'
    the quick red fox
    the lazy dog
    the quick blue fox
    Last edited by schragge; March 26th, 2013 at 02:57 PM.

  3. #3
    Join Date
    Dec 2012
    Beans
    74

    Re: Replacing an entire line based on finding part of it

    Quote Originally Posted by schragge View Post
    Sure
    Code:
    $ for color in red brown blue;do echo the quick $color fox;done
    the quick red fox
    the quick brown fox
    the quick blue fox
    Code:
    $ for color in red brown blue;do echo the quick $color fox;done | sed '/brown/s/.*/the lazy dog/'
    the quick red fox
    the lazy dog
    the quick blue fox
    you sir are a star, thank-you very much

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •