I have a html file that I want to trim. I want to remove a section from the beginning all the way to a given string, and from another string to the end. How do I do that, preferably using sed?
+6
A:
With GNU sed
:
sed '/mark1/,/mark2/d;/mark3/,$d'
this
abc
def
mark1
ghi
jkl
mno
mark2
pqr
stu
mark3
vwx
yz
becomes
abc
def
pqr
stu
Dennis Williamson
2010-04-11 20:42:38
Would add that it's only working with properly formatted (pattern starts and ends on the same line!) html files.
Zsolt Botykai
2010-04-11 20:52:47
That's true and normally html should be manipulated with a robust library tool (and luck and wind direction are also involved).
Dennis Williamson
2010-04-11 21:13:00
A:
you can use awk
$ cat file
mark1 dsf
abc
def
before mark2 after
blah mark1
ghi
jkl
mno
wirds mark2 here
pqr
stu
mark3
vwx
yz
$ awk -vRS="mark2" '/mark1/{gsub("mark1.*","")}/mark3/{ gsub("mark3.*","");print;f=1 } !f ' file
after
blah
here
pqr
stu
ghostdog74
2010-04-11 23:59:48