Delete ranges of lines, but skip the comments which come in between the lines
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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
add a comment |Â
up vote
2
down vote
favorite
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
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 thesed '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
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
text-processing sed
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 thesed '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
add a comment |Â
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 thesed '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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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
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
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
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
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
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