PDA

View Full Version : help with 'sed' command




scan
Apr 23, 2007, 11:21 AM
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 ']>'



ChrisA
Apr 23, 2007, 12:11 PM
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



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?

toddburch
Apr 23, 2007, 01:10 PM
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

scan
Apr 23, 2007, 01:15 PM
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?

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.

scan
Apr 23, 2007, 01:16 PM
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


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.

balamw
Apr 23, 2007, 01:22 PM
I think I can only manipulate a line at a time with sed, so I will feed it a script file.

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

savar
Apr 23, 2007, 01:46 PM
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.

Correct: it is not possible to match an expression across a line break with sed.