sed - delete lines taking patterns from external file
up vote
1
down vote
favorite
I'm looking for a way to delete certain lines in a file taking patterns from external file. The best way would be to use sed.
I was trying several articles, like this, but they doesn't answer my task.
Say i have a text.file with:
Adam
Belle
Candy
Donald
Eve
And a pattern.file has:
Don*
Candy
With grep -fv pattern.file text.file
i get exactly what i want, but only in view:
Adam
Belle
Eve
I cannot redirect the filtered output to a new file. Therefore, i need a way to be able modify the original text.file, deleting all the rows that match the pattern(s) from external file.
In my real usecase the text.file contains non-alphabethical order, not always capitalized, so creating a generic regex for 'sed' is not possible.
What would be the best approach to pass patterns for lines deletion from an external file?
linux bash sed
add a comment |
up vote
1
down vote
favorite
I'm looking for a way to delete certain lines in a file taking patterns from external file. The best way would be to use sed.
I was trying several articles, like this, but they doesn't answer my task.
Say i have a text.file with:
Adam
Belle
Candy
Donald
Eve
And a pattern.file has:
Don*
Candy
With grep -fv pattern.file text.file
i get exactly what i want, but only in view:
Adam
Belle
Eve
I cannot redirect the filtered output to a new file. Therefore, i need a way to be able modify the original text.file, deleting all the rows that match the pattern(s) from external file.
In my real usecase the text.file contains non-alphabethical order, not always capitalized, so creating a generic regex for 'sed' is not possible.
What would be the best approach to pass patterns for lines deletion from an external file?
linux bash sed
Why can't you redirect the output to a new file?sed -i
creates a new file behind the scenes, anyway.
– choroba
Nov 17 at 20:05
@choroba - loss of privileges. Can only modify
– faceless
Nov 17 at 20:14
1
So maybesponge
, to redirect the output to the same file.
– Kamil Maciorowski
Nov 17 at 20:16
1
This might work:grep -vf pattern.file text.file >foo; cp foo text.file & rm foo
– Cyrus
Nov 18 at 8:44
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm looking for a way to delete certain lines in a file taking patterns from external file. The best way would be to use sed.
I was trying several articles, like this, but they doesn't answer my task.
Say i have a text.file with:
Adam
Belle
Candy
Donald
Eve
And a pattern.file has:
Don*
Candy
With grep -fv pattern.file text.file
i get exactly what i want, but only in view:
Adam
Belle
Eve
I cannot redirect the filtered output to a new file. Therefore, i need a way to be able modify the original text.file, deleting all the rows that match the pattern(s) from external file.
In my real usecase the text.file contains non-alphabethical order, not always capitalized, so creating a generic regex for 'sed' is not possible.
What would be the best approach to pass patterns for lines deletion from an external file?
linux bash sed
I'm looking for a way to delete certain lines in a file taking patterns from external file. The best way would be to use sed.
I was trying several articles, like this, but they doesn't answer my task.
Say i have a text.file with:
Adam
Belle
Candy
Donald
Eve
And a pattern.file has:
Don*
Candy
With grep -fv pattern.file text.file
i get exactly what i want, but only in view:
Adam
Belle
Eve
I cannot redirect the filtered output to a new file. Therefore, i need a way to be able modify the original text.file, deleting all the rows that match the pattern(s) from external file.
In my real usecase the text.file contains non-alphabethical order, not always capitalized, so creating a generic regex for 'sed' is not possible.
What would be the best approach to pass patterns for lines deletion from an external file?
linux bash sed
linux bash sed
edited Nov 17 at 20:05
choroba
12.9k13039
12.9k13039
asked Nov 17 at 20:00
faceless
103
103
Why can't you redirect the output to a new file?sed -i
creates a new file behind the scenes, anyway.
– choroba
Nov 17 at 20:05
@choroba - loss of privileges. Can only modify
– faceless
Nov 17 at 20:14
1
So maybesponge
, to redirect the output to the same file.
– Kamil Maciorowski
Nov 17 at 20:16
1
This might work:grep -vf pattern.file text.file >foo; cp foo text.file & rm foo
– Cyrus
Nov 18 at 8:44
add a comment |
Why can't you redirect the output to a new file?sed -i
creates a new file behind the scenes, anyway.
– choroba
Nov 17 at 20:05
@choroba - loss of privileges. Can only modify
– faceless
Nov 17 at 20:14
1
So maybesponge
, to redirect the output to the same file.
– Kamil Maciorowski
Nov 17 at 20:16
1
This might work:grep -vf pattern.file text.file >foo; cp foo text.file & rm foo
– Cyrus
Nov 18 at 8:44
Why can't you redirect the output to a new file?
sed -i
creates a new file behind the scenes, anyway.– choroba
Nov 17 at 20:05
Why can't you redirect the output to a new file?
sed -i
creates a new file behind the scenes, anyway.– choroba
Nov 17 at 20:05
@choroba - loss of privileges. Can only modify
– faceless
Nov 17 at 20:14
@choroba - loss of privileges. Can only modify
– faceless
Nov 17 at 20:14
1
1
So maybe
sponge
, to redirect the output to the same file.– Kamil Maciorowski
Nov 17 at 20:16
So maybe
sponge
, to redirect the output to the same file.– Kamil Maciorowski
Nov 17 at 20:16
1
1
This might work:
grep -vf pattern.file text.file >foo; cp foo text.file & rm foo
– Cyrus
Nov 18 at 8:44
This might work:
grep -vf pattern.file text.file >foo; cp foo text.file & rm foo
– Cyrus
Nov 18 at 8:44
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
With GNU sed and bash:
sed -i -f <(sed 's|.*|/&/d|' pattern.file) text.file
This sort-of works, but not necessarily for the right reasons. (1) It does an unanchored match, so if the pattern file contains "Alex", it will remove "Alexander" and "Alexandra" from the text file. (2) It expects the patterns to be regular expressions. It does not look like this is what the OP intended. For example, for the given pattern file, "Douglass" would be deleted, becauseDon*
matches any line that containsDo
. (If it happens to be followed by a string ofn
’s, that doesn’t matter.) (3) The user should be warned that, if the pattern file contains slashes (/
), this will fail.
– Scott
Nov 18 at 9:20
Thanks @Cyrus, this is what i was looking for! Scott, thatnk you as well, the provided answer suits my particular pattern management, but i will take your notes in attention.
– faceless
Nov 18 at 9:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
With GNU sed and bash:
sed -i -f <(sed 's|.*|/&/d|' pattern.file) text.file
This sort-of works, but not necessarily for the right reasons. (1) It does an unanchored match, so if the pattern file contains "Alex", it will remove "Alexander" and "Alexandra" from the text file. (2) It expects the patterns to be regular expressions. It does not look like this is what the OP intended. For example, for the given pattern file, "Douglass" would be deleted, becauseDon*
matches any line that containsDo
. (If it happens to be followed by a string ofn
’s, that doesn’t matter.) (3) The user should be warned that, if the pattern file contains slashes (/
), this will fail.
– Scott
Nov 18 at 9:20
Thanks @Cyrus, this is what i was looking for! Scott, thatnk you as well, the provided answer suits my particular pattern management, but i will take your notes in attention.
– faceless
Nov 18 at 9:41
add a comment |
up vote
1
down vote
accepted
With GNU sed and bash:
sed -i -f <(sed 's|.*|/&/d|' pattern.file) text.file
This sort-of works, but not necessarily for the right reasons. (1) It does an unanchored match, so if the pattern file contains "Alex", it will remove "Alexander" and "Alexandra" from the text file. (2) It expects the patterns to be regular expressions. It does not look like this is what the OP intended. For example, for the given pattern file, "Douglass" would be deleted, becauseDon*
matches any line that containsDo
. (If it happens to be followed by a string ofn
’s, that doesn’t matter.) (3) The user should be warned that, if the pattern file contains slashes (/
), this will fail.
– Scott
Nov 18 at 9:20
Thanks @Cyrus, this is what i was looking for! Scott, thatnk you as well, the provided answer suits my particular pattern management, but i will take your notes in attention.
– faceless
Nov 18 at 9:41
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
With GNU sed and bash:
sed -i -f <(sed 's|.*|/&/d|' pattern.file) text.file
With GNU sed and bash:
sed -i -f <(sed 's|.*|/&/d|' pattern.file) text.file
answered Nov 18 at 8:32
Cyrus
3,74111024
3,74111024
This sort-of works, but not necessarily for the right reasons. (1) It does an unanchored match, so if the pattern file contains "Alex", it will remove "Alexander" and "Alexandra" from the text file. (2) It expects the patterns to be regular expressions. It does not look like this is what the OP intended. For example, for the given pattern file, "Douglass" would be deleted, becauseDon*
matches any line that containsDo
. (If it happens to be followed by a string ofn
’s, that doesn’t matter.) (3) The user should be warned that, if the pattern file contains slashes (/
), this will fail.
– Scott
Nov 18 at 9:20
Thanks @Cyrus, this is what i was looking for! Scott, thatnk you as well, the provided answer suits my particular pattern management, but i will take your notes in attention.
– faceless
Nov 18 at 9:41
add a comment |
This sort-of works, but not necessarily for the right reasons. (1) It does an unanchored match, so if the pattern file contains "Alex", it will remove "Alexander" and "Alexandra" from the text file. (2) It expects the patterns to be regular expressions. It does not look like this is what the OP intended. For example, for the given pattern file, "Douglass" would be deleted, becauseDon*
matches any line that containsDo
. (If it happens to be followed by a string ofn
’s, that doesn’t matter.) (3) The user should be warned that, if the pattern file contains slashes (/
), this will fail.
– Scott
Nov 18 at 9:20
Thanks @Cyrus, this is what i was looking for! Scott, thatnk you as well, the provided answer suits my particular pattern management, but i will take your notes in attention.
– faceless
Nov 18 at 9:41
This sort-of works, but not necessarily for the right reasons. (1) It does an unanchored match, so if the pattern file contains "Alex", it will remove "Alexander" and "Alexandra" from the text file. (2) It expects the patterns to be regular expressions. It does not look like this is what the OP intended. For example, for the given pattern file, "Douglass" would be deleted, because
Don*
matches any line that contains Do
. (If it happens to be followed by a string of n
’s, that doesn’t matter.) (3) The user should be warned that, if the pattern file contains slashes (/
), this will fail.– Scott
Nov 18 at 9:20
This sort-of works, but not necessarily for the right reasons. (1) It does an unanchored match, so if the pattern file contains "Alex", it will remove "Alexander" and "Alexandra" from the text file. (2) It expects the patterns to be regular expressions. It does not look like this is what the OP intended. For example, for the given pattern file, "Douglass" would be deleted, because
Don*
matches any line that contains Do
. (If it happens to be followed by a string of n
’s, that doesn’t matter.) (3) The user should be warned that, if the pattern file contains slashes (/
), this will fail.– Scott
Nov 18 at 9:20
Thanks @Cyrus, this is what i was looking for! Scott, thatnk you as well, the provided answer suits my particular pattern management, but i will take your notes in attention.
– faceless
Nov 18 at 9:41
Thanks @Cyrus, this is what i was looking for! Scott, thatnk you as well, the provided answer suits my particular pattern management, but i will take your notes in attention.
– faceless
Nov 18 at 9:41
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1376305%2fsed-delete-lines-taking-patterns-from-external-file%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
Why can't you redirect the output to a new file?
sed -i
creates a new file behind the scenes, anyway.– choroba
Nov 17 at 20:05
@choroba - loss of privileges. Can only modify
– faceless
Nov 17 at 20:14
1
So maybe
sponge
, to redirect the output to the same file.– Kamil Maciorowski
Nov 17 at 20:16
1
This might work:
grep -vf pattern.file text.file >foo; cp foo text.file & rm foo
– Cyrus
Nov 18 at 8:44