help with 'sed' command

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

  1. scan macrumors 6502

    Joined:
    Oct 24, 2005
    #1
    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

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #2
    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

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    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.

    Todd
     
  4. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #4
    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

    Joined:
    Oct 24, 2005
    #5

    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

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    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.)

    B
     
  7. savar macrumors 68000

    savar

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

Share This Page