Remove everything between specified characters including multi lines
up vote
2
down vote
favorite
I have a file with contents like so:
## this must go ##
## also
this
must go
##
hello world
##and this one
too##
I want to remove all between ##, including multi lines, so I am left just with hello world
This removes only part that is on one line:
sed -i.bak 's/##.*##//g' myfile
How to remove multi line stuff too?
P.S Im on MAC
regex perl awk sed
add a comment |
up vote
2
down vote
favorite
I have a file with contents like so:
## this must go ##
## also
this
must go
##
hello world
##and this one
too##
I want to remove all between ##, including multi lines, so I am left just with hello world
This removes only part that is on one line:
sed -i.bak 's/##.*##//g' myfile
How to remove multi line stuff too?
P.S Im on MAC
regex perl awk sed
Probably perl solution will be simpler:perl -0pe 's/##.*?##R*//gs' file > newfile
(demo).
– Wiktor Stribiżew
Nov 22 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 at 14:55
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a file with contents like so:
## this must go ##
## also
this
must go
##
hello world
##and this one
too##
I want to remove all between ##, including multi lines, so I am left just with hello world
This removes only part that is on one line:
sed -i.bak 's/##.*##//g' myfile
How to remove multi line stuff too?
P.S Im on MAC
regex perl awk sed
I have a file with contents like so:
## this must go ##
## also
this
must go
##
hello world
##and this one
too##
I want to remove all between ##, including multi lines, so I am left just with hello world
This removes only part that is on one line:
sed -i.bak 's/##.*##//g' myfile
How to remove multi line stuff too?
P.S Im on MAC
regex perl awk sed
regex perl awk sed
edited Nov 22 at 15:47
RavinderSingh13
24.8k41437
24.8k41437
asked Nov 22 at 14:16
bukowski
87862144
87862144
Probably perl solution will be simpler:perl -0pe 's/##.*?##R*//gs' file > newfile
(demo).
– Wiktor Stribiżew
Nov 22 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 at 14:55
add a comment |
Probably perl solution will be simpler:perl -0pe 's/##.*?##R*//gs' file > newfile
(demo).
– Wiktor Stribiżew
Nov 22 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 at 14:55
Probably perl solution will be simpler:
perl -0pe 's/##.*?##R*//gs' file > newfile
(demo).– Wiktor Stribiżew
Nov 22 at 14:26
Probably perl solution will be simpler:
perl -0pe 's/##.*?##R*//gs' file > newfile
(demo).– Wiktor Stribiżew
Nov 22 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 at 14:51
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 at 14:55
Posted with explanation.
– Wiktor Stribiżew
Nov 22 at 14:55
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
You may use perl
to achieve what you want:
perl -0pe 's/##.*?##R*//gs' file > newfile
See the online demo
The 0
argument makes it possible to find matches across lines.
The pattern matches
##
- two#
symbols
.*?
- any 0+ chars (even line break chars due to thes
modifier) as few as possible
##
- two#
symbols
R*
- any 0+ line break sequences.
add a comment |
up vote
1
down vote
It should be very easy task for awk
(in case you are ok with it). Could you please try following, will add explanation shortly too.
awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file
Adding a non-one liner form of solution too now.
awk '
/^##.*##$/{
next
}
/^##$/{
flag=""
next
}
/^##/ && !/##$/{
flag=1
}
flag{
next
}
1
' Input_file
add a comment |
up vote
1
down vote
Give a try to this:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett
The test:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
hello world
add a comment |
up vote
0
down vote
This might work for you (GNU sed):
sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file
The -z
option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ##
followed by zero or more non-#
's. The second part matches a zero or more group of characters consisting of a single #
followed by a non-#
followed by zero or more non-#
's. The third part matches ##
and a possible newline. This regexp removes such matches globally throughout the file.
This can by shortened slightly by using the -r
option to sweeten the final offering to:
sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file
If the version of sed does not offer either options, then another solution is:
sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file
It should by noted, that in the example above, all ##
's either start or end at the beginning or end of a line and so the solution below might also fit the bill:
sed 's/^##/,/##$/d' file
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You may use perl
to achieve what you want:
perl -0pe 's/##.*?##R*//gs' file > newfile
See the online demo
The 0
argument makes it possible to find matches across lines.
The pattern matches
##
- two#
symbols
.*?
- any 0+ chars (even line break chars due to thes
modifier) as few as possible
##
- two#
symbols
R*
- any 0+ line break sequences.
add a comment |
up vote
2
down vote
accepted
You may use perl
to achieve what you want:
perl -0pe 's/##.*?##R*//gs' file > newfile
See the online demo
The 0
argument makes it possible to find matches across lines.
The pattern matches
##
- two#
symbols
.*?
- any 0+ chars (even line break chars due to thes
modifier) as few as possible
##
- two#
symbols
R*
- any 0+ line break sequences.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You may use perl
to achieve what you want:
perl -0pe 's/##.*?##R*//gs' file > newfile
See the online demo
The 0
argument makes it possible to find matches across lines.
The pattern matches
##
- two#
symbols
.*?
- any 0+ chars (even line break chars due to thes
modifier) as few as possible
##
- two#
symbols
R*
- any 0+ line break sequences.
You may use perl
to achieve what you want:
perl -0pe 's/##.*?##R*//gs' file > newfile
See the online demo
The 0
argument makes it possible to find matches across lines.
The pattern matches
##
- two#
symbols
.*?
- any 0+ chars (even line break chars due to thes
modifier) as few as possible
##
- two#
symbols
R*
- any 0+ line break sequences.
answered Nov 22 at 14:53
Wiktor Stribiżew
304k16123200
304k16123200
add a comment |
add a comment |
up vote
1
down vote
It should be very easy task for awk
(in case you are ok with it). Could you please try following, will add explanation shortly too.
awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file
Adding a non-one liner form of solution too now.
awk '
/^##.*##$/{
next
}
/^##$/{
flag=""
next
}
/^##/ && !/##$/{
flag=1
}
flag{
next
}
1
' Input_file
add a comment |
up vote
1
down vote
It should be very easy task for awk
(in case you are ok with it). Could you please try following, will add explanation shortly too.
awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file
Adding a non-one liner form of solution too now.
awk '
/^##.*##$/{
next
}
/^##$/{
flag=""
next
}
/^##/ && !/##$/{
flag=1
}
flag{
next
}
1
' Input_file
add a comment |
up vote
1
down vote
up vote
1
down vote
It should be very easy task for awk
(in case you are ok with it). Could you please try following, will add explanation shortly too.
awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file
Adding a non-one liner form of solution too now.
awk '
/^##.*##$/{
next
}
/^##$/{
flag=""
next
}
/^##/ && !/##$/{
flag=1
}
flag{
next
}
1
' Input_file
It should be very easy task for awk
(in case you are ok with it). Could you please try following, will add explanation shortly too.
awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file
Adding a non-one liner form of solution too now.
awk '
/^##.*##$/{
next
}
/^##$/{
flag=""
next
}
/^##/ && !/##$/{
flag=1
}
flag{
next
}
1
' Input_file
answered Nov 22 at 14:27
RavinderSingh13
24.8k41437
24.8k41437
add a comment |
add a comment |
up vote
1
down vote
Give a try to this:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett
The test:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
hello world
add a comment |
up vote
1
down vote
Give a try to this:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett
The test:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
hello world
add a comment |
up vote
1
down vote
up vote
1
down vote
Give a try to this:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett
The test:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
hello world
Give a try to this:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett
The test:
sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile
hello world
edited Nov 22 at 16:26
answered Nov 22 at 14:31
Jay jargot
1,8771410
1,8771410
add a comment |
add a comment |
up vote
0
down vote
This might work for you (GNU sed):
sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file
The -z
option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ##
followed by zero or more non-#
's. The second part matches a zero or more group of characters consisting of a single #
followed by a non-#
followed by zero or more non-#
's. The third part matches ##
and a possible newline. This regexp removes such matches globally throughout the file.
This can by shortened slightly by using the -r
option to sweeten the final offering to:
sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file
If the version of sed does not offer either options, then another solution is:
sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file
It should by noted, that in the example above, all ##
's either start or end at the beginning or end of a line and so the solution below might also fit the bill:
sed 's/^##/,/##$/d' file
add a comment |
up vote
0
down vote
This might work for you (GNU sed):
sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file
The -z
option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ##
followed by zero or more non-#
's. The second part matches a zero or more group of characters consisting of a single #
followed by a non-#
followed by zero or more non-#
's. The third part matches ##
and a possible newline. This regexp removes such matches globally throughout the file.
This can by shortened slightly by using the -r
option to sweeten the final offering to:
sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file
If the version of sed does not offer either options, then another solution is:
sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file
It should by noted, that in the example above, all ##
's either start or end at the beginning or end of a line and so the solution below might also fit the bill:
sed 's/^##/,/##$/d' file
add a comment |
up vote
0
down vote
up vote
0
down vote
This might work for you (GNU sed):
sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file
The -z
option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ##
followed by zero or more non-#
's. The second part matches a zero or more group of characters consisting of a single #
followed by a non-#
followed by zero or more non-#
's. The third part matches ##
and a possible newline. This regexp removes such matches globally throughout the file.
This can by shortened slightly by using the -r
option to sweeten the final offering to:
sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file
If the version of sed does not offer either options, then another solution is:
sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file
It should by noted, that in the example above, all ##
's either start or end at the beginning or end of a line and so the solution below might also fit the bill:
sed 's/^##/,/##$/d' file
This might work for you (GNU sed):
sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file
The -z
option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ##
followed by zero or more non-#
's. The second part matches a zero or more group of characters consisting of a single #
followed by a non-#
followed by zero or more non-#
's. The third part matches ##
and a possible newline. This regexp removes such matches globally throughout the file.
This can by shortened slightly by using the -r
option to sweeten the final offering to:
sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file
If the version of sed does not offer either options, then another solution is:
sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file
It should by noted, that in the example above, all ##
's either start or end at the beginning or end of a line and so the solution below might also fit the bill:
sed 's/^##/,/##$/d' file
edited Nov 23 at 8:52
answered Nov 23 at 8:42
potong
34.8k42960
34.8k42960
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432910%2fremove-everything-between-specified-characters-including-multi-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Probably perl solution will be simpler:
perl -0pe 's/##.*?##R*//gs' file > newfile
(demo).– Wiktor Stribiżew
Nov 22 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 at 14:55