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?










share|improve this question
























  • 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

















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?










share|improve this question
























  • 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















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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




















  • 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


















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












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





share|improve this answer





















  • 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











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: 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
});


}
});














 

draft saved


draft discarded


















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

























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





share|improve this answer





















  • 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















up vote
1
down vote



accepted










With GNU sed and bash:



sed -i -f <(sed 's|.*|/&/d|' pattern.file) text.file





share|improve this answer





















  • 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













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





share|improve this answer












With GNU sed and bash:



sed -i -f <(sed 's|.*|/&/d|' pattern.file) text.file






share|improve this answer












share|improve this answer



share|improve this answer










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, 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


















  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Сан-Квентин

Алькесар

Josef Freinademetz