grep specific number from lines 5 but not 25 or 52 and so on
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
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 Jan 9 at 12:15
don_crissti
51.8k15141168
51.8k15141168
asked Jan 9 at 12:13
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 a grep
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.
Jan 9 at 14:05
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
Jan 9 at 20:59
add a comment |
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
4
This would be better asgrep -E "^5s"
in case there's a tab and not a space.
– terdon♦
Jan 9 at 13:50
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
.
2
That will also match lines starting with5:
,5%
,5|
etc.
– terdon♦
Jan 9 at 13:49
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 a grep
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.
Jan 9 at 14:05
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
Jan 9 at 20:59
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 a grep
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.
Jan 9 at 14:05
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
Jan 9 at 20:59
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 a grep
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 a grep
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 Jan 12 at 19:22
Jeff Schaller♦
44.6k1162145
44.6k1162145
answered Jan 9 at 12:24
wurtelwurtel
11.1k11628
11.1k11628
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
Jan 9 at 14:05
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
Jan 9 at 20:59
add a comment |
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
Jan 9 at 14:05
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
Jan 9 at 20:59
1
1
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
Jan 9 at 14:05
thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.
– Dave.
Jan 9 at 14:05
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
Jan 9 at 20:59
@Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.
– bishop
Jan 9 at 20:59
add a comment |
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
4
This would be better asgrep -E "^5s"
in case there's a tab and not a space.
– terdon♦
Jan 9 at 13:50
add a comment |
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
4
This would be better asgrep -E "^5s"
in case there's a tab and not a space.
– terdon♦
Jan 9 at 13:50
add a comment |
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
You could try with a regex (first char in line) and including the space:
grep -E "^5 "
answered Jan 9 at 12:21
rbrtflrrbrtflr
613
613
4
This would be better asgrep -E "^5s"
in case there's a tab and not a space.
– terdon♦
Jan 9 at 13:50
add a comment |
4
This would be better asgrep -E "^5s"
in case there's a tab and not a space.
– terdon♦
Jan 9 at 13:50
4
4
This would be better as
grep -E "^5s"
in case there's a tab and not a space.– terdon♦
Jan 9 at 13:50
This would be better as
grep -E "^5s"
in case there's a tab and not a space.– terdon♦
Jan 9 at 13:50
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
.
2
That will also match lines starting with5:
,5%
,5|
etc.
– terdon♦
Jan 9 at 13:49
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
.
2
That will also match lines starting with5:
,5%
,5|
etc.
– terdon♦
Jan 9 at 13:49
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
.
Try the following:
query-auth-system | grep "^5\>"
^
: means "match at start of line
\>
: matches a word-boundary. So it will match5
, but not50
.
answered Jan 9 at 12:19
RalfRalf
36718
36718
2
That will also match lines starting with5:
,5%
,5|
etc.
– terdon♦
Jan 9 at 13:49
add a comment |
2
That will also match lines starting with5:
,5%
,5|
etc.
– terdon♦
Jan 9 at 13:49
2
2
That will also match lines starting with
5:
, 5%
, 5|
etc.– terdon♦
Jan 9 at 13:49
That will also match lines starting with
5:
, 5%
, 5|
etc.– terdon♦
Jan 9 at 13:49
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