What is the correct way to format this grep?
I am having trouble figuring out where to place the in this command.
grep "^D.*(A1|A2|A3)$" input.txt > output.txt
I'm searching for each line that starts with a D
and ends with A1, A2, or A3, which is at the end of the line.
command-line grep regex
add a comment |
I am having trouble figuring out where to place the in this command.
grep "^D.*(A1|A2|A3)$" input.txt > output.txt
I'm searching for each line that starts with a D
and ends with A1, A2, or A3, which is at the end of the line.
command-line grep regex
Try the-E
option.
– Gunnar Hjalmarsson
Jan 25 at 23:23
1
For this particular case, you could use a simple character range^D.*A[1-3]$
– steeldriver
Jan 26 at 2:09
add a comment |
I am having trouble figuring out where to place the in this command.
grep "^D.*(A1|A2|A3)$" input.txt > output.txt
I'm searching for each line that starts with a D
and ends with A1, A2, or A3, which is at the end of the line.
command-line grep regex
I am having trouble figuring out where to place the in this command.
grep "^D.*(A1|A2|A3)$" input.txt > output.txt
I'm searching for each line that starts with a D
and ends with A1, A2, or A3, which is at the end of the line.
command-line grep regex
command-line grep regex
edited Jan 25 at 23:39
wjandrea
9,13942363
9,13942363
asked Jan 25 at 23:00
Sim GrocSim Groc
31
31
Try the-E
option.
– Gunnar Hjalmarsson
Jan 25 at 23:23
1
For this particular case, you could use a simple character range^D.*A[1-3]$
– steeldriver
Jan 26 at 2:09
add a comment |
Try the-E
option.
– Gunnar Hjalmarsson
Jan 25 at 23:23
1
For this particular case, you could use a simple character range^D.*A[1-3]$
– steeldriver
Jan 26 at 2:09
Try the
-E
option.– Gunnar Hjalmarsson
Jan 25 at 23:23
Try the
-E
option.– Gunnar Hjalmarsson
Jan 25 at 23:23
1
1
For this particular case, you could use a simple character range
^D.*A[1-3]$
– steeldriver
Jan 26 at 2:09
For this particular case, you could use a simple character range
^D.*A[1-3]$
– steeldriver
Jan 26 at 2:09
add a comment |
3 Answers
3
active
oldest
votes
You just needed to escape the or bars - you were almost there:
grep "^D.*(A1|A2|A3)$"
Note you can also use egrep instead of all the escapes:
egrep "^D.*(A1|A2|A3)$"
add a comment |
You need to escape the pipes (|
).
$ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
Or use option -E
for extended regex, then you don't need to escape anything.
$ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
We can use also-P
instead of-E
and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?
– pa4080
Jan 26 at 14:36
add a comment |
Here is sed
implementation, that uses the combination of the option -n
and the command p
:
sed -rn '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*A[1-3]$/p' in-file
option
-r
,--regexp-extended
: use extended regular expressions in the script.option
-n
,--quiet
,--silent
: suppress automatic printing of pattern space.command
p
: print the current pattern space.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2faskubuntu.com%2fquestions%2f1112944%2fwhat-is-the-correct-way-to-format-this-grep%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You just needed to escape the or bars - you were almost there:
grep "^D.*(A1|A2|A3)$"
Note you can also use egrep instead of all the escapes:
egrep "^D.*(A1|A2|A3)$"
add a comment |
You just needed to escape the or bars - you were almost there:
grep "^D.*(A1|A2|A3)$"
Note you can also use egrep instead of all the escapes:
egrep "^D.*(A1|A2|A3)$"
add a comment |
You just needed to escape the or bars - you were almost there:
grep "^D.*(A1|A2|A3)$"
Note you can also use egrep instead of all the escapes:
egrep "^D.*(A1|A2|A3)$"
You just needed to escape the or bars - you were almost there:
grep "^D.*(A1|A2|A3)$"
Note you can also use egrep instead of all the escapes:
egrep "^D.*(A1|A2|A3)$"
answered Jan 25 at 23:36
Eric MintzEric Mintz
662312
662312
add a comment |
add a comment |
You need to escape the pipes (|
).
$ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
Or use option -E
for extended regex, then you don't need to escape anything.
$ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
We can use also-P
instead of-E
and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?
– pa4080
Jan 26 at 14:36
add a comment |
You need to escape the pipes (|
).
$ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
Or use option -E
for extended regex, then you don't need to escape anything.
$ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
We can use also-P
instead of-E
and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?
– pa4080
Jan 26 at 14:36
add a comment |
You need to escape the pipes (|
).
$ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
Or use option -E
for extended regex, then you don't need to escape anything.
$ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
You need to escape the pipes (|
).
$ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
Or use option -E
for extended regex, then you don't need to escape anything.
$ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
DA1
DA2
DA3
answered Jan 25 at 23:33
wjandreawjandrea
9,13942363
9,13942363
We can use also-P
instead of-E
and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?
– pa4080
Jan 26 at 14:36
add a comment |
We can use also-P
instead of-E
and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?
– pa4080
Jan 26 at 14:36
We can use also
-P
instead of -E
and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?– pa4080
Jan 26 at 14:36
We can use also
-P
instead of -E
and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?– pa4080
Jan 26 at 14:36
add a comment |
Here is sed
implementation, that uses the combination of the option -n
and the command p
:
sed -rn '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*A[1-3]$/p' in-file
option
-r
,--regexp-extended
: use extended regular expressions in the script.option
-n
,--quiet
,--silent
: suppress automatic printing of pattern space.command
p
: print the current pattern space.
add a comment |
Here is sed
implementation, that uses the combination of the option -n
and the command p
:
sed -rn '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*A[1-3]$/p' in-file
option
-r
,--regexp-extended
: use extended regular expressions in the script.option
-n
,--quiet
,--silent
: suppress automatic printing of pattern space.command
p
: print the current pattern space.
add a comment |
Here is sed
implementation, that uses the combination of the option -n
and the command p
:
sed -rn '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*A[1-3]$/p' in-file
option
-r
,--regexp-extended
: use extended regular expressions in the script.option
-n
,--quiet
,--silent
: suppress automatic printing of pattern space.command
p
: print the current pattern space.
Here is sed
implementation, that uses the combination of the option -n
and the command p
:
sed -rn '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*(A1|A2|A3)$/p' in-file
sed -n '/^D.*A[1-3]$/p' in-file
option
-r
,--regexp-extended
: use extended regular expressions in the script.option
-n
,--quiet
,--silent
: suppress automatic printing of pattern space.command
p
: print the current pattern space.
edited Jan 26 at 14:26
answered Jan 26 at 13:37
pa4080pa4080
14.2k52668
14.2k52668
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2faskubuntu.com%2fquestions%2f1112944%2fwhat-is-the-correct-way-to-format-this-grep%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
Try the
-E
option.– Gunnar Hjalmarsson
Jan 25 at 23:23
1
For this particular case, you could use a simple character range
^D.*A[1-3]$
– steeldriver
Jan 26 at 2:09