Thread: Mass file edit

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Mass file edit


    Hi,

    I am trying to mass edit a bunch of files using sed . Below is what I have in my script

    abcdef
    Some sentence # comment
    asashkhhjk

    I need to add a new line after the "Some sentence" and add a new sentence there for all the files in the directory. As of now I am trying to do the following

    sed -i -e "s/Some sentence\t# comment/Some sentence\t# comment\nNew Sentence"

    Is there an easy way to do this inseatd of replacing sentence1 and then adding sentence 1\nsentence 2
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,448
    Rep Power
    1751
    You should be able to use a 'back reference' in the replacement:
    Originally Posted by man page
    s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub- expressions in the regexp.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo