grep specific number from lines 5 but not 25 or 52 and so on
I need to find a user for a userid. The return from the authentication system is as follows. Now with a bash script I need to extract the line with the exact number. Not 5 and 25 ..
------------
ID LOGIN
------------
28 user1
25 user2
5 user3
If I use grep 5 I do get 2 lines but I need the line with "5".
Any ideas ?
text-processing
add a comment |
I need to find a user for a userid. The return from the authentication system is as follows. Now with a bash script I need to extract the line with the exact number. Not 5 and 25 ..
------------
ID LOGIN
------------
28 user1
25 user2
5 user3
If I use grep 5 I do get 2 lines but I need the line with "5".
Any ideas ?
text-processing
add a comment |
I need to find a user for a userid. The return from the authentication system is as follows. Now with a bash script I need to extract the line with the exact number. Not 5 and 25 ..
------------
ID LOGIN
------------
28 user1
25 user2
5 user3
If I use grep 5 I do get 2 lines but I need the line with "5".
Any ideas ?
text-processing
I need to find a user for a userid. The return from the authentication system is as follows. Now with a bash script I need to extract the line with the exact number. Not 5 and 25 ..
------------
ID LOGIN
------------
28 user1
25 user2
5 user3
If I use grep 5 I do get 2 lines but I need the line with "5".
Any ideas ?
text-processing
text-processing
edited 2 days ago
don_crissti
50.1k15132162
50.1k15132162
asked 2 days ago
Dave.Dave.
241
241
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
There are several ways of doing it.
IMHO the best way is to use awk, which is useful when dealing with fields.
If you want agrep based solution, I would do:
grep -w '^5'
The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.
The awk solution would look like:
awk '$1 == 5'
If you want only the username, which is the second column:
awk '$1 == 5 {print $2}'
If you're searching for a string and not a numeric value, enclose the string in double quotes:
awk '$1 == "abc" {print $2}'
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
2 days ago
2
@Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.
– bishop
2 days ago
add a comment |
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
New contributor
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
This would be better asgrep -E "^5s"in case there's a tab and not a space.
– terdon♦
2 days ago
add a comment |
Try the following:
query-auth-system | grep "^5\>"
^: means "match at start of line
\>: matches a word-boundary. So it will match5, but not50.
New contributor
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
That will also match lines starting with5:,5%,5|etc.
– terdon♦
2 days ago
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%2f493445%2fgrep-specific-number-from-lines-5-but-not-25-or-52-and-so-on%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
There are several ways of doing it.
IMHO the best way is to use awk, which is useful when dealing with fields.
If you want agrep based solution, I would do:
grep -w '^5'
The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.
The awk solution would look like:
awk '$1 == 5'
If you want only the username, which is the second column:
awk '$1 == 5 {print $2}'
If you're searching for a string and not a numeric value, enclose the string in double quotes:
awk '$1 == "abc" {print $2}'
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
2 days ago
2
@Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.
– bishop
2 days ago
add a comment |
There are several ways of doing it.
IMHO the best way is to use awk, which is useful when dealing with fields.
If you want agrep based solution, I would do:
grep -w '^5'
The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.
The awk solution would look like:
awk '$1 == 5'
If you want only the username, which is the second column:
awk '$1 == 5 {print $2}'
If you're searching for a string and not a numeric value, enclose the string in double quotes:
awk '$1 == "abc" {print $2}'
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
2 days ago
2
@Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.
– bishop
2 days ago
add a comment |
There are several ways of doing it.
IMHO the best way is to use awk, which is useful when dealing with fields.
If you want agrep based solution, I would do:
grep -w '^5'
The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.
The awk solution would look like:
awk '$1 == 5'
If you want only the username, which is the second column:
awk '$1 == 5 {print $2}'
If you're searching for a string and not a numeric value, enclose the string in double quotes:
awk '$1 == "abc" {print $2}'
There are several ways of doing it.
IMHO the best way is to use awk, which is useful when dealing with fields.
If you want agrep based solution, I would do:
grep -w '^5'
The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.
The awk solution would look like:
awk '$1 == 5'
If you want only the username, which is the second column:
awk '$1 == 5 {print $2}'
If you're searching for a string and not a numeric value, enclose the string in double quotes:
awk '$1 == "abc" {print $2}'
edited 2 days ago
terdon♦
129k32253428
129k32253428
answered 2 days ago
wurtelwurtel
10k11425
10k11425
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
2 days ago
2
@Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.
– bishop
2 days ago
add a comment |
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
2 days ago
2
@Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.
– bishop
2 days ago
1
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
2 days ago
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
2 days ago
2
2
@Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.
– bishop
2 days ago
@Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.
– bishop
2 days ago
add a comment |
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
New contributor
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
This would be better asgrep -E "^5s"in case there's a tab and not a space.
– terdon♦
2 days ago
add a comment |
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
New contributor
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
This would be better asgrep -E "^5s"in case there's a tab and not a space.
– terdon♦
2 days ago
add a comment |
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
New contributor
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
New contributor
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
rbrtflrrbrtflr
613
613
New contributor
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
rbrtflr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
This would be better asgrep -E "^5s"in case there's a tab and not a space.
– terdon♦
2 days ago
add a comment |
4
This would be better asgrep -E "^5s"in case there's a tab and not a space.
– terdon♦
2 days ago
4
4
This would be better as
grep -E "^5s" in case there's a tab and not a space.– terdon♦
2 days ago
This would be better as
grep -E "^5s" in case there's a tab and not a space.– terdon♦
2 days ago
add a comment |
Try the following:
query-auth-system | grep "^5\>"
^: means "match at start of line
\>: matches a word-boundary. So it will match5, but not50.
New contributor
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
That will also match lines starting with5:,5%,5|etc.
– terdon♦
2 days ago
add a comment |
Try the following:
query-auth-system | grep "^5\>"
^: means "match at start of line
\>: matches a word-boundary. So it will match5, but not50.
New contributor
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
That will also match lines starting with5:,5%,5|etc.
– terdon♦
2 days ago
add a comment |
Try the following:
query-auth-system | grep "^5\>"
^: means "match at start of line
\>: matches a word-boundary. So it will match5, but not50.
New contributor
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Try the following:
query-auth-system | grep "^5\>"
^: means "match at start of line
\>: matches a word-boundary. So it will match5, but not50.
New contributor
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
RalfRalf
3257
3257
New contributor
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Ralf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
That will also match lines starting with5:,5%,5|etc.
– terdon♦
2 days ago
add a comment |
2
That will also match lines starting with5:,5%,5|etc.
– terdon♦
2 days ago
2
2
That will also match lines starting with
5:, 5%, 5| etc.– terdon♦
2 days ago
That will also match lines starting with
5:, 5%, 5| etc.– terdon♦
2 days ago
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.
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%2f493445%2fgrep-specific-number-from-lines-5-but-not-25-or-52-and-so-on%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