help with 'sed' command

Discussion in 'Mac Programming' started by scan, Apr 23, 2007.

  1. scan macrumors 6502

    Oct 24, 2005
    I have a ton of files that I have to modified and I haven't done this stuff in years. I have an idea what my sed command should look like

    sed '/^<!Blah*.[and then a bunch of stuff i dont' care including new lines]*]>$ d' file

    as you can see, i'm not sure what the expression should look like to look for anything including tabs, spaces, new lines, etc

    so an example doc would have:

    <!blah something something
    <something something>
    <could have something>
    ]> //end

    I have to delete all those lines.

    edit: just a little more clarification. I want to look for something that begins with "<!blah" and delete all the lines until ']>' is found including ']>'
  2. ChrisA macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    try this

    sed s/foo/bar/ infile > outfile

    In your example line you left of the "s" which is the "substitute" editor command. In your case you want "foo" to match your string you want removes and "bar" to be null Like this "s/foo//" which will remove all the "foos"
    foo should be a regular expression that matches what you want removed.

    Have you read the sed man page?
  3. toddburch macrumors 6502a

    Dec 4, 2006
    Katy, Texas
    I don't think you want '/^<!Blah* ...$/, since a match will occur zero or more times (the * quantifier), and thus every line will match, whether it starts with <!Blah or not.

    Perhaps you want /^<!Blah+[.]*]>$/

    which says: at the start of the line, look for '<!Blah' and it must occur at least once (but can occur multiple times), followed by any character ([.]) any number of times (*), followed by ']>' at the very end of the line.

    (I've never used sed, but I am in the midst of reading "Mastering Regular Expressions...) ;)

    Not sure of you have to escape the last ] or not.

    Check the book and run some tests (or back everything up) before you kick it off.

  4. scan thread starter macrumors 6502

    Oct 24, 2005
    That is not what I'm looking for. Yes, I've read the man pages. I've used unix for years. Just don't remember sed expressions from back in the day. Thanks anyways.
  5. scan thread starter macrumors 6502

    Oct 24, 2005

    After working with this for awhile. I think I may have a solution. I think I can only manipulate a line at a time with sed, so I will feed it a script file. Thanks for your help.
  6. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England
    I've got a perl script somewhere that essentially does this if you don't find a good solution. (look for a string signifying the beginning of a comment and stop output until the end of the comment is detected.)

  7. savar macrumors 68000


    Jun 6, 2003
    District of Columbia
    Correct: it is not possible to match an expression across a line break with sed.

Share This Page