Keep lines containing “list of different words” like pattern [duplicate]












0
















This question already has an answer here:




  • How to make sed remove lines not matched by a substitution

    4 answers



  • Boolean OR in sed regex

    4 answers




How can I keep all lines matching all those words
toto OR titi OR clic OR SOMETHING and delete any other lines?



If I do sed '/toto/ p ' file I cannot select titi for example.



What I am looking for is something similar to a Perl Regular expression as
^ (word1|word2|word3|andsoon).*. However, I need it for sed because it will be integrated into a bigger sed script.



The goal is to keep all lines starting with word where word is any word from a set of words.










share|improve this question















marked as duplicate by Wiktor Stribiżew, Inian, tripleee, Michael Dodd, Toby Speight Nov 23 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • How is your larger sed script called? If it is called just as sed, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called as sed -n you actually have to select the lines you are interested in. But my overall suggestion would be set your first awk rule to delete the lines that do not match.

    – kvantour
    Nov 23 '18 at 10:08
















0
















This question already has an answer here:




  • How to make sed remove lines not matched by a substitution

    4 answers



  • Boolean OR in sed regex

    4 answers




How can I keep all lines matching all those words
toto OR titi OR clic OR SOMETHING and delete any other lines?



If I do sed '/toto/ p ' file I cannot select titi for example.



What I am looking for is something similar to a Perl Regular expression as
^ (word1|word2|word3|andsoon).*. However, I need it for sed because it will be integrated into a bigger sed script.



The goal is to keep all lines starting with word where word is any word from a set of words.










share|improve this question















marked as duplicate by Wiktor Stribiżew, Inian, tripleee, Michael Dodd, Toby Speight Nov 23 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • How is your larger sed script called? If it is called just as sed, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called as sed -n you actually have to select the lines you are interested in. But my overall suggestion would be set your first awk rule to delete the lines that do not match.

    – kvantour
    Nov 23 '18 at 10:08














0












0








0









This question already has an answer here:




  • How to make sed remove lines not matched by a substitution

    4 answers



  • Boolean OR in sed regex

    4 answers




How can I keep all lines matching all those words
toto OR titi OR clic OR SOMETHING and delete any other lines?



If I do sed '/toto/ p ' file I cannot select titi for example.



What I am looking for is something similar to a Perl Regular expression as
^ (word1|word2|word3|andsoon).*. However, I need it for sed because it will be integrated into a bigger sed script.



The goal is to keep all lines starting with word where word is any word from a set of words.










share|improve this question

















This question already has an answer here:




  • How to make sed remove lines not matched by a substitution

    4 answers



  • Boolean OR in sed regex

    4 answers




How can I keep all lines matching all those words
toto OR titi OR clic OR SOMETHING and delete any other lines?



If I do sed '/toto/ p ' file I cannot select titi for example.



What I am looking for is something similar to a Perl Regular expression as
^ (word1|word2|word3|andsoon).*. However, I need it for sed because it will be integrated into a bigger sed script.



The goal is to keep all lines starting with word where word is any word from a set of words.





This question already has an answer here:




  • How to make sed remove lines not matched by a substitution

    4 answers



  • Boolean OR in sed regex

    4 answers








sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:25









kvantour

10.1k41731




10.1k41731










asked Nov 23 '18 at 9:17









francois Pfrancois P

176211




176211




marked as duplicate by Wiktor Stribiżew, Inian, tripleee, Michael Dodd, Toby Speight Nov 23 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Wiktor Stribiżew, Inian, tripleee, Michael Dodd, Toby Speight Nov 23 '18 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • How is your larger sed script called? If it is called just as sed, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called as sed -n you actually have to select the lines you are interested in. But my overall suggestion would be set your first awk rule to delete the lines that do not match.

    – kvantour
    Nov 23 '18 at 10:08



















  • How is your larger sed script called? If it is called just as sed, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called as sed -n you actually have to select the lines you are interested in. But my overall suggestion would be set your first awk rule to delete the lines that do not match.

    – kvantour
    Nov 23 '18 at 10:08

















How is your larger sed script called? If it is called just as sed, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called as sed -n you actually have to select the lines you are interested in. But my overall suggestion would be set your first awk rule to delete the lines that do not match.

– kvantour
Nov 23 '18 at 10:08





How is your larger sed script called? If it is called just as sed, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called as sed -n you actually have to select the lines you are interested in. But my overall suggestion would be set your first awk rule to delete the lines that do not match.

– kvantour
Nov 23 '18 at 10:08












1 Answer
1






active

oldest

votes


















1














The answer here depends a bit on how your master script is called. Imagine you have a file with the following content:



foo
car
bar


and you are interested in the lines matching "foo" and "bar", then you can do:



sed '/foo|bar/!d'
sed -n '/foo|bar/!d;p'
sed -n '/foo|bar/p'


all these will output:



foo
bar


If you would just do:



sed '/foo|bar/p'


you actually duplicate the lines.



foo
foo
car
bar
bar


As you see, there is a bit of different handling depending on the usage of the -n flag.




-n, --quiet, --silent suppress automatic printing of pattern space



source: man sed




In general, my suggestion is to delete the lines you don't need at the beginning of your sed script.






share|improve this answer
























  • nearly that ... so I adapted it as sed -i -E '/^(word1.*|word2|word3.*|etc.)$/ !d' thanks

    – francois P
    Nov 23 '18 at 10:33






  • 1





    @francoisP don't forget to escape your | and brackets, it is /^(word1|word2|word3|...).*$/!d

    – kvantour
    Nov 23 '18 at 10:36




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The answer here depends a bit on how your master script is called. Imagine you have a file with the following content:



foo
car
bar


and you are interested in the lines matching "foo" and "bar", then you can do:



sed '/foo|bar/!d'
sed -n '/foo|bar/!d;p'
sed -n '/foo|bar/p'


all these will output:



foo
bar


If you would just do:



sed '/foo|bar/p'


you actually duplicate the lines.



foo
foo
car
bar
bar


As you see, there is a bit of different handling depending on the usage of the -n flag.




-n, --quiet, --silent suppress automatic printing of pattern space



source: man sed




In general, my suggestion is to delete the lines you don't need at the beginning of your sed script.






share|improve this answer
























  • nearly that ... so I adapted it as sed -i -E '/^(word1.*|word2|word3.*|etc.)$/ !d' thanks

    – francois P
    Nov 23 '18 at 10:33






  • 1





    @francoisP don't forget to escape your | and brackets, it is /^(word1|word2|word3|...).*$/!d

    – kvantour
    Nov 23 '18 at 10:36


















1














The answer here depends a bit on how your master script is called. Imagine you have a file with the following content:



foo
car
bar


and you are interested in the lines matching "foo" and "bar", then you can do:



sed '/foo|bar/!d'
sed -n '/foo|bar/!d;p'
sed -n '/foo|bar/p'


all these will output:



foo
bar


If you would just do:



sed '/foo|bar/p'


you actually duplicate the lines.



foo
foo
car
bar
bar


As you see, there is a bit of different handling depending on the usage of the -n flag.




-n, --quiet, --silent suppress automatic printing of pattern space



source: man sed




In general, my suggestion is to delete the lines you don't need at the beginning of your sed script.






share|improve this answer
























  • nearly that ... so I adapted it as sed -i -E '/^(word1.*|word2|word3.*|etc.)$/ !d' thanks

    – francois P
    Nov 23 '18 at 10:33






  • 1





    @francoisP don't forget to escape your | and brackets, it is /^(word1|word2|word3|...).*$/!d

    – kvantour
    Nov 23 '18 at 10:36
















1












1








1







The answer here depends a bit on how your master script is called. Imagine you have a file with the following content:



foo
car
bar


and you are interested in the lines matching "foo" and "bar", then you can do:



sed '/foo|bar/!d'
sed -n '/foo|bar/!d;p'
sed -n '/foo|bar/p'


all these will output:



foo
bar


If you would just do:



sed '/foo|bar/p'


you actually duplicate the lines.



foo
foo
car
bar
bar


As you see, there is a bit of different handling depending on the usage of the -n flag.




-n, --quiet, --silent suppress automatic printing of pattern space



source: man sed




In general, my suggestion is to delete the lines you don't need at the beginning of your sed script.






share|improve this answer













The answer here depends a bit on how your master script is called. Imagine you have a file with the following content:



foo
car
bar


and you are interested in the lines matching "foo" and "bar", then you can do:



sed '/foo|bar/!d'
sed -n '/foo|bar/!d;p'
sed -n '/foo|bar/p'


all these will output:



foo
bar


If you would just do:



sed '/foo|bar/p'


you actually duplicate the lines.



foo
foo
car
bar
bar


As you see, there is a bit of different handling depending on the usage of the -n flag.




-n, --quiet, --silent suppress automatic printing of pattern space



source: man sed




In general, my suggestion is to delete the lines you don't need at the beginning of your sed script.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 10:16









kvantourkvantour

10.1k41731




10.1k41731













  • nearly that ... so I adapted it as sed -i -E '/^(word1.*|word2|word3.*|etc.)$/ !d' thanks

    – francois P
    Nov 23 '18 at 10:33






  • 1





    @francoisP don't forget to escape your | and brackets, it is /^(word1|word2|word3|...).*$/!d

    – kvantour
    Nov 23 '18 at 10:36





















  • nearly that ... so I adapted it as sed -i -E '/^(word1.*|word2|word3.*|etc.)$/ !d' thanks

    – francois P
    Nov 23 '18 at 10:33






  • 1





    @francoisP don't forget to escape your | and brackets, it is /^(word1|word2|word3|...).*$/!d

    – kvantour
    Nov 23 '18 at 10:36



















nearly that ... so I adapted it as sed -i -E '/^(word1.*|word2|word3.*|etc.)$/ !d' thanks

– francois P
Nov 23 '18 at 10:33





nearly that ... so I adapted it as sed -i -E '/^(word1.*|word2|word3.*|etc.)$/ !d' thanks

– francois P
Nov 23 '18 at 10:33




1




1





@francoisP don't forget to escape your | and brackets, it is /^(word1|word2|word3|...).*$/!d

– kvantour
Nov 23 '18 at 10:36







@francoisP don't forget to escape your | and brackets, it is /^(word1|word2|word3|...).*$/!d

– kvantour
Nov 23 '18 at 10:36







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()