How to grep all result such that the sub-pattern may or may not containing in the target pattern?
Clash Royale CLAN TAG#URR8PPP
Suppose I have search the string which give like the following result
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
How I can write generic syntax for grep such it match all 3 string.
I have done like this
^.*w+d[?[0]?[]]?.knownKeyWord.*$
But I think for indexing eg [1]
is not written in good way, how can I achieve so that even i replace [1]
with [2342jdsjf]
, I don't have to change the syntax much.
grep
add a comment |
Suppose I have search the string which give like the following result
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
How I can write generic syntax for grep such it match all 3 string.
I have done like this
^.*w+d[?[0]?[]]?.knownKeyWord.*$
But I think for indexing eg [1]
is not written in good way, how can I achieve so that even i replace [1]
with [2342jdsjf]
, I don't have to change the syntax much.
grep
add a comment |
Suppose I have search the string which give like the following result
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
How I can write generic syntax for grep such it match all 3 string.
I have done like this
^.*w+d[?[0]?[]]?.knownKeyWord.*$
But I think for indexing eg [1]
is not written in good way, how can I achieve so that even i replace [1]
with [2342jdsjf]
, I don't have to change the syntax much.
grep
Suppose I have search the string which give like the following result
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
How I can write generic syntax for grep such it match all 3 string.
I have done like this
^.*w+d[?[0]?[]]?.knownKeyWord.*$
But I think for indexing eg [1]
is not written in good way, how can I achieve so that even i replace [1]
with [2342jdsjf]
, I don't have to change the syntax much.
grep
grep
edited Dec 17 at 12:09
asked Dec 17 at 12:04
Vinay Sah
32
32
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Using an extended regular expression:
$ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
This would extract any line containing a string on the format
XXXNNN[YYY].knownKeyWord
or
XXXNNN.knownKeyWord
where XXX
is any non-empty alphanumeric string (that may also include _
), NNN
is any string of (one or more) digits, and YYY
is anything not including a ]
.
Use grep
with -x
if matches are to be complete lines. Use -w
if matches are supposed to be complete words (i.e. not as a substring of something else).
Just using sed
to show what each part of the regular expression is matching:
$ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
<anything><1><><.knownKeyWord>
<anything><2><><.knownKeyWord>
<anything><3><[1]><.knownKeyWord>
add a comment |
Try this,
grep -w 'knownKeyWord$' file.txt
From man
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f489459%2fhow-to-grep-all-result-such-that-the-sub-pattern-may-or-may-not-containing-in-th%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using an extended regular expression:
$ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
This would extract any line containing a string on the format
XXXNNN[YYY].knownKeyWord
or
XXXNNN.knownKeyWord
where XXX
is any non-empty alphanumeric string (that may also include _
), NNN
is any string of (one or more) digits, and YYY
is anything not including a ]
.
Use grep
with -x
if matches are to be complete lines. Use -w
if matches are supposed to be complete words (i.e. not as a substring of something else).
Just using sed
to show what each part of the regular expression is matching:
$ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
<anything><1><><.knownKeyWord>
<anything><2><><.knownKeyWord>
<anything><3><[1]><.knownKeyWord>
add a comment |
Using an extended regular expression:
$ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
This would extract any line containing a string on the format
XXXNNN[YYY].knownKeyWord
or
XXXNNN.knownKeyWord
where XXX
is any non-empty alphanumeric string (that may also include _
), NNN
is any string of (one or more) digits, and YYY
is anything not including a ]
.
Use grep
with -x
if matches are to be complete lines. Use -w
if matches are supposed to be complete words (i.e. not as a substring of something else).
Just using sed
to show what each part of the regular expression is matching:
$ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
<anything><1><><.knownKeyWord>
<anything><2><><.knownKeyWord>
<anything><3><[1]><.knownKeyWord>
add a comment |
Using an extended regular expression:
$ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
This would extract any line containing a string on the format
XXXNNN[YYY].knownKeyWord
or
XXXNNN.knownKeyWord
where XXX
is any non-empty alphanumeric string (that may also include _
), NNN
is any string of (one or more) digits, and YYY
is anything not including a ]
.
Use grep
with -x
if matches are to be complete lines. Use -w
if matches are supposed to be complete words (i.e. not as a substring of something else).
Just using sed
to show what each part of the regular expression is matching:
$ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
<anything><1><><.knownKeyWord>
<anything><2><><.knownKeyWord>
<anything><3><[1]><.knownKeyWord>
Using an extended regular expression:
$ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord
This would extract any line containing a string on the format
XXXNNN[YYY].knownKeyWord
or
XXXNNN.knownKeyWord
where XXX
is any non-empty alphanumeric string (that may also include _
), NNN
is any string of (one or more) digits, and YYY
is anything not including a ]
.
Use grep
with -x
if matches are to be complete lines. Use -w
if matches are supposed to be complete words (i.e. not as a substring of something else).
Just using sed
to show what each part of the regular expression is matching:
$ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
<anything><1><><.knownKeyWord>
<anything><2><><.knownKeyWord>
<anything><3><[1]><.knownKeyWord>
answered Dec 17 at 13:01
Kusalananda
121k16229372
121k16229372
add a comment |
add a comment |
Try this,
grep -w 'knownKeyWord$' file.txt
From man
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.
add a comment |
Try this,
grep -w 'knownKeyWord$' file.txt
From man
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.
add a comment |
Try this,
grep -w 'knownKeyWord$' file.txt
From man
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.
Try this,
grep -w 'knownKeyWord$' file.txt
From man
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.
edited Dec 17 at 13:11
answered Dec 17 at 13:05
msp9011
3,74343863
3,74343863
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f489459%2fhow-to-grep-all-result-such-that-the-sub-pattern-may-or-may-not-containing-in-th%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