Keep lines containing “list of different words” like pattern [duplicate]
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.
sed
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.
add a comment |
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.
sed
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 largersed
script called? If it is called just assed
, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called assed -n
you actually have to select the lines you are interested in. But my overall suggestion would be set your firstawk
rule to delete the lines that do not match.
– kvantour
Nov 23 '18 at 10:08
add a comment |
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.
sed
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
sed
edited Nov 23 '18 at 11:25
kvantour
8,19631230
8,19631230
asked Nov 23 '18 at 9:17
francois Pfrancois P
172211
172211
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 largersed
script called? If it is called just assed
, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called assed -n
you actually have to select the lines you are interested in. But my overall suggestion would be set your firstawk
rule to delete the lines that do not match.
– kvantour
Nov 23 '18 at 10:08
add a comment |
How is your largersed
script called? If it is called just assed
, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called assed -n
you actually have to select the lines you are interested in. But my overall suggestion would be set your firstawk
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered Nov 23 '18 at 10:16
kvantourkvantour
8,19631230
8,19631230
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
add a comment |
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
add a comment |
How is your larger
sed
script called? If it is called just assed
, then the task at hand would be to delete all lines that do not match to corresponding expressions, if it is called assed -n
you actually have to select the lines you are interested in. But my overall suggestion would be set your firstawk
rule to delete the lines that do not match.– kvantour
Nov 23 '18 at 10:08