Delete ranges of lines, but skip the comments which come in between the lines

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
2












I have a file with the following contents:



Windows user
I love windows
Windows 10
# I like it
# I want to keep these two lines
Just started with my job
New to shell scripting as well
New to Mac
Please help!

#EOF


I want to delete all the lines:



starting from "I love windows" to "New to shell scripting as well", but keep the comments that are present between these lines.



So, the desired output should appear as below:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF


I used sed command to delete the range of lines using the line numbers



sed '2,7d' file


But this command deletes the comments as well, which I want preserved.










share|improve this question























  • Somehow I have the feeling you are not looking for an Ubuntu solution. Actually not a Linux solution at all. Please make sure you are not to waist people's time, looking for answers that you will not be able to confirm.
    – Jacob Vlijm
    3 hours ago







  • 1




    @JacobVlijm with the sed '2,6d' file command s/he came with and the the output s/he got, I think s/he is able to use that command either in Ubuntu or WSL or Cygwin or ...
    – Î±Ò“sнιη
    3 hours ago











  • IF it is not Ubuntu, this is typically an off topic situation @αғsнιη
    – Jacob Vlijm
    2 hours ago











  • Thanks to all those who helped me answer this question, without considering it irrelevant to the forum. I got what I was looking for, thanks a lot!
    – Akshay Nandi
    1 hour ago














up vote
2
down vote

favorite
2












I have a file with the following contents:



Windows user
I love windows
Windows 10
# I like it
# I want to keep these two lines
Just started with my job
New to shell scripting as well
New to Mac
Please help!

#EOF


I want to delete all the lines:



starting from "I love windows" to "New to shell scripting as well", but keep the comments that are present between these lines.



So, the desired output should appear as below:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF


I used sed command to delete the range of lines using the line numbers



sed '2,7d' file


But this command deletes the comments as well, which I want preserved.










share|improve this question























  • Somehow I have the feeling you are not looking for an Ubuntu solution. Actually not a Linux solution at all. Please make sure you are not to waist people's time, looking for answers that you will not be able to confirm.
    – Jacob Vlijm
    3 hours ago







  • 1




    @JacobVlijm with the sed '2,6d' file command s/he came with and the the output s/he got, I think s/he is able to use that command either in Ubuntu or WSL or Cygwin or ...
    – Î±Ò“sнιη
    3 hours ago











  • IF it is not Ubuntu, this is typically an off topic situation @αғsнιη
    – Jacob Vlijm
    2 hours ago











  • Thanks to all those who helped me answer this question, without considering it irrelevant to the forum. I got what I was looking for, thanks a lot!
    – Akshay Nandi
    1 hour ago












up vote
2
down vote

favorite
2









up vote
2
down vote

favorite
2






2





I have a file with the following contents:



Windows user
I love windows
Windows 10
# I like it
# I want to keep these two lines
Just started with my job
New to shell scripting as well
New to Mac
Please help!

#EOF


I want to delete all the lines:



starting from "I love windows" to "New to shell scripting as well", but keep the comments that are present between these lines.



So, the desired output should appear as below:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF


I used sed command to delete the range of lines using the line numbers



sed '2,7d' file


But this command deletes the comments as well, which I want preserved.










share|improve this question















I have a file with the following contents:



Windows user
I love windows
Windows 10
# I like it
# I want to keep these two lines
Just started with my job
New to shell scripting as well
New to Mac
Please help!

#EOF


I want to delete all the lines:



starting from "I love windows" to "New to shell scripting as well", but keep the comments that are present between these lines.



So, the desired output should appear as below:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF


I used sed command to delete the range of lines using the line numbers



sed '2,7d' file


But this command deletes the comments as well, which I want preserved.







text-processing sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









αғsнιη

23.3k2191152




23.3k2191152










asked 3 hours ago









Akshay Nandi

206




206











  • Somehow I have the feeling you are not looking for an Ubuntu solution. Actually not a Linux solution at all. Please make sure you are not to waist people's time, looking for answers that you will not be able to confirm.
    – Jacob Vlijm
    3 hours ago







  • 1




    @JacobVlijm with the sed '2,6d' file command s/he came with and the the output s/he got, I think s/he is able to use that command either in Ubuntu or WSL or Cygwin or ...
    – Î±Ò“sнιη
    3 hours ago











  • IF it is not Ubuntu, this is typically an off topic situation @αғsнιη
    – Jacob Vlijm
    2 hours ago











  • Thanks to all those who helped me answer this question, without considering it irrelevant to the forum. I got what I was looking for, thanks a lot!
    – Akshay Nandi
    1 hour ago
















  • Somehow I have the feeling you are not looking for an Ubuntu solution. Actually not a Linux solution at all. Please make sure you are not to waist people's time, looking for answers that you will not be able to confirm.
    – Jacob Vlijm
    3 hours ago







  • 1




    @JacobVlijm with the sed '2,6d' file command s/he came with and the the output s/he got, I think s/he is able to use that command either in Ubuntu or WSL or Cygwin or ...
    – Î±Ò“sнιη
    3 hours ago











  • IF it is not Ubuntu, this is typically an off topic situation @αғsнιη
    – Jacob Vlijm
    2 hours ago











  • Thanks to all those who helped me answer this question, without considering it irrelevant to the forum. I got what I was looking for, thanks a lot!
    – Akshay Nandi
    1 hour ago















Somehow I have the feeling you are not looking for an Ubuntu solution. Actually not a Linux solution at all. Please make sure you are not to waist people's time, looking for answers that you will not be able to confirm.
– Jacob Vlijm
3 hours ago





Somehow I have the feeling you are not looking for an Ubuntu solution. Actually not a Linux solution at all. Please make sure you are not to waist people's time, looking for answers that you will not be able to confirm.
– Jacob Vlijm
3 hours ago





1




1




@JacobVlijm with the sed '2,6d' file command s/he came with and the the output s/he got, I think s/he is able to use that command either in Ubuntu or WSL or Cygwin or ...
– Î±Ò“sнιη
3 hours ago





@JacobVlijm with the sed '2,6d' file command s/he came with and the the output s/he got, I think s/he is able to use that command either in Ubuntu or WSL or Cygwin or ...
– Î±Ò“sнιη
3 hours ago













IF it is not Ubuntu, this is typically an off topic situation @αғsнιη
– Jacob Vlijm
2 hours ago





IF it is not Ubuntu, this is typically an off topic situation @αғsнιη
– Jacob Vlijm
2 hours ago













Thanks to all those who helped me answer this question, without considering it irrelevant to the forum. I got what I was looking for, thanks a lot!
– Akshay Nandi
1 hour ago




Thanks to all those who helped me answer this question, without considering it irrelevant to the forum. I got what I was looking for, thanks a lot!
– Akshay Nandi
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










Try using:



sed '2,7/^[[:blank:]]*#/!d' infile


This is normally deleting lines from 2~7 but not the lines that starts with a hash as known as a commented line.



The [[:blank:]] character class is used to match and keep those lines that it's a commented line but took whitespaces at behind too either zero or more whitespace.



To be more specifically using the patterns given:



sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' infile


Standard-complaint sed solution would be:



sed -e '2,7' -e '/^[[:space:]]*#/!d' -e '' infile


The output is:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF





share|improve this answer


















  • 1




    you can (should) use a regex range: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' -- that more exactly meets the requirements
    – glenn jackman
    2 hours ago










  • I will implement the solution and let you both know the results, thank you
    – Akshay Nandi
    2 hours ago










  • I tried the following: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' But it throws an error: "/I love windows/,/New t ...": extra characters at the end of d command
    – Akshay Nandi
    1 hour ago







  • 1




    glad it to worked!
    – Î±Ò“sнιη
    1 hour ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
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',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1075020%2fdelete-ranges-of-lines-but-skip-the-comments-which-come-in-between-the-lines%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










Try using:



sed '2,7/^[[:blank:]]*#/!d' infile


This is normally deleting lines from 2~7 but not the lines that starts with a hash as known as a commented line.



The [[:blank:]] character class is used to match and keep those lines that it's a commented line but took whitespaces at behind too either zero or more whitespace.



To be more specifically using the patterns given:



sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' infile


Standard-complaint sed solution would be:



sed -e '2,7' -e '/^[[:space:]]*#/!d' -e '' infile


The output is:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF





share|improve this answer


















  • 1




    you can (should) use a regex range: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' -- that more exactly meets the requirements
    – glenn jackman
    2 hours ago










  • I will implement the solution and let you both know the results, thank you
    – Akshay Nandi
    2 hours ago










  • I tried the following: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' But it throws an error: "/I love windows/,/New t ...": extra characters at the end of d command
    – Akshay Nandi
    1 hour ago







  • 1




    glad it to worked!
    – Î±Ò“sнιη
    1 hour ago














up vote
4
down vote



accepted










Try using:



sed '2,7/^[[:blank:]]*#/!d' infile


This is normally deleting lines from 2~7 but not the lines that starts with a hash as known as a commented line.



The [[:blank:]] character class is used to match and keep those lines that it's a commented line but took whitespaces at behind too either zero or more whitespace.



To be more specifically using the patterns given:



sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' infile


Standard-complaint sed solution would be:



sed -e '2,7' -e '/^[[:space:]]*#/!d' -e '' infile


The output is:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF





share|improve this answer


















  • 1




    you can (should) use a regex range: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' -- that more exactly meets the requirements
    – glenn jackman
    2 hours ago










  • I will implement the solution and let you both know the results, thank you
    – Akshay Nandi
    2 hours ago










  • I tried the following: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' But it throws an error: "/I love windows/,/New t ...": extra characters at the end of d command
    – Akshay Nandi
    1 hour ago







  • 1




    glad it to worked!
    – Î±Ò“sнιη
    1 hour ago












up vote
4
down vote



accepted







up vote
4
down vote



accepted






Try using:



sed '2,7/^[[:blank:]]*#/!d' infile


This is normally deleting lines from 2~7 but not the lines that starts with a hash as known as a commented line.



The [[:blank:]] character class is used to match and keep those lines that it's a commented line but took whitespaces at behind too either zero or more whitespace.



To be more specifically using the patterns given:



sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' infile


Standard-complaint sed solution would be:



sed -e '2,7' -e '/^[[:space:]]*#/!d' -e '' infile


The output is:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF





share|improve this answer














Try using:



sed '2,7/^[[:blank:]]*#/!d' infile


This is normally deleting lines from 2~7 but not the lines that starts with a hash as known as a commented line.



The [[:blank:]] character class is used to match and keep those lines that it's a commented line but took whitespaces at behind too either zero or more whitespace.



To be more specifically using the patterns given:



sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' infile


Standard-complaint sed solution would be:



sed -e '2,7' -e '/^[[:space:]]*#/!d' -e '' infile


The output is:



Windows user
# I like it
# I want to keep these two lines
New to Mac
Please help!

#EOF






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 3 hours ago









αғsнιη

23.3k2191152




23.3k2191152







  • 1




    you can (should) use a regex range: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' -- that more exactly meets the requirements
    – glenn jackman
    2 hours ago










  • I will implement the solution and let you both know the results, thank you
    – Akshay Nandi
    2 hours ago










  • I tried the following: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' But it throws an error: "/I love windows/,/New t ...": extra characters at the end of d command
    – Akshay Nandi
    1 hour ago







  • 1




    glad it to worked!
    – Î±Ò“sнιη
    1 hour ago












  • 1




    you can (should) use a regex range: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' -- that more exactly meets the requirements
    – glenn jackman
    2 hours ago










  • I will implement the solution and let you both know the results, thank you
    – Akshay Nandi
    2 hours ago










  • I tried the following: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' But it throws an error: "/I love windows/,/New t ...": extra characters at the end of d command
    – Akshay Nandi
    1 hour ago







  • 1




    glad it to worked!
    – Î±Ò“sнιη
    1 hour ago







1




1




you can (should) use a regex range: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' -- that more exactly meets the requirements
– glenn jackman
2 hours ago




you can (should) use a regex range: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' -- that more exactly meets the requirements
– glenn jackman
2 hours ago












I will implement the solution and let you both know the results, thank you
– Akshay Nandi
2 hours ago




I will implement the solution and let you both know the results, thank you
– Akshay Nandi
2 hours ago












I tried the following: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' But it throws an error: "/I love windows/,/New t ...": extra characters at the end of d command
– Akshay Nandi
1 hour ago





I tried the following: sed '/I love windows/,/New to shell scripting as well/ /^[[:blank:]]*#/!d' But it throws an error: "/I love windows/,/New t ...": extra characters at the end of d command
– Akshay Nandi
1 hour ago





1




1




glad it to worked!
– Î±Ò“sнιη
1 hour ago




glad it to worked!
– Î±Ò“sнιη
1 hour ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1075020%2fdelete-ranges-of-lines-but-skip-the-comments-which-come-in-between-the-lines%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery