Remove everything between specified characters including multi lines
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 |
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 '18 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 '18 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 '18 at 14:55
add a comment |
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 '18 at 15:47
RavinderSingh13
28.9k41638
28.9k41638
asked Nov 22 '18 at 14:16
bukowskibukowski
91862146
91862146
Probably perl solution will be simpler:perl -0pe 's/##.*?##R*//gs' file > newfile(demo).
– Wiktor Stribiżew
Nov 22 '18 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 '18 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 '18 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 '18 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 '18 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 '18 at 14:55
Probably perl solution will be simpler:
perl -0pe 's/##.*?##R*//gs' file > newfile (demo).– Wiktor Stribiżew
Nov 22 '18 at 14:26
Probably perl solution will be simpler:
perl -0pe 's/##.*?##R*//gs' file > newfile (demo).– Wiktor Stribiżew
Nov 22 '18 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 '18 at 14:51
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 '18 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 '18 at 14:55
Posted with explanation.
– Wiktor Stribiżew
Nov 22 '18 at 14:55
add a comment |
4 Answers
4
active
oldest
votes
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 thesmodifier) as few as possible
##- two#symbols
R*- any 0+ line break sequences.
add a comment |
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 |
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 |
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 thesmodifier) as few as possible
##- two#symbols
R*- any 0+ line break sequences.
add a comment |
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 thesmodifier) as few as possible
##- two#symbols
R*- any 0+ line break sequences.
add a comment |
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 thesmodifier) 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 thesmodifier) as few as possible
##- two#symbols
R*- any 0+ line break sequences.
answered Nov 22 '18 at 14:53
Wiktor StribiżewWiktor Stribiżew
320k16140222
320k16140222
add a comment |
add a comment |
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 |
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 |
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 '18 at 14:27
RavinderSingh13RavinderSingh13
28.9k41638
28.9k41638
add a comment |
add a comment |
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 |
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 |
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 '18 at 16:26
answered Nov 22 '18 at 14:31
Jay jargotJay jargot
1,9491510
1,9491510
add a comment |
add a comment |
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 |
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 |
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 '18 at 8:52
answered Nov 23 '18 at 8:42
potongpotong
35.9k43061
35.9k43061
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.
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 '18 at 14:26
@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 '18 at 14:51
Posted with explanation.
– Wiktor Stribiżew
Nov 22 '18 at 14:55