Grep Showing Extra Lines
up vote
1
down vote
favorite
So I have a text file:
4556 4618 7843 8732
4532 0861 1932 5122
3478 893* 6788 6312
5440 3173 8207 0451 67886
6011 2966 7184 4668
3678 3905 5323
2389 4387 9336 2783
239 235 453 3458
182 534 654 765
4485 0721 1308 2759
46759 543 2345
I want to grep only the numbers that have 4 digits together, 4 times in a row (seperated by a space).
For example: 4556 4618 7843 8732
I am using: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})" test.txt
Which shows me:
4556 4618 7843 8732
4532 0861 1932 5122
5440 3173 8207 0451 67886
6011 2966 7184 4668
4485 0721 1308 2759
Using this there is an extra line that shouldn't appear, where there is a 5th set of numbers that has 5 digits on the end.
So I used: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
But this only gave me two results instead of the 4 it should:
4556 4618 7843 8732
4485 0721 1308 2759
Can someone tell me what I'm doing wrong?
linux grep
add a comment |
up vote
1
down vote
favorite
So I have a text file:
4556 4618 7843 8732
4532 0861 1932 5122
3478 893* 6788 6312
5440 3173 8207 0451 67886
6011 2966 7184 4668
3678 3905 5323
2389 4387 9336 2783
239 235 453 3458
182 534 654 765
4485 0721 1308 2759
46759 543 2345
I want to grep only the numbers that have 4 digits together, 4 times in a row (seperated by a space).
For example: 4556 4618 7843 8732
I am using: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})" test.txt
Which shows me:
4556 4618 7843 8732
4532 0861 1932 5122
5440 3173 8207 0451 67886
6011 2966 7184 4668
4485 0721 1308 2759
Using this there is an extra line that shouldn't appear, where there is a 5th set of numbers that has 5 digits on the end.
So I used: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
But this only gave me two results instead of the 4 it should:
4556 4618 7843 8732
4485 0721 1308 2759
Can someone tell me what I'm doing wrong?
linux grep
You also appear to be missing2389 4387 9336 2783
-- are extra spaces the eliminating factor?
– glenn jackman
37 mins ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
So I have a text file:
4556 4618 7843 8732
4532 0861 1932 5122
3478 893* 6788 6312
5440 3173 8207 0451 67886
6011 2966 7184 4668
3678 3905 5323
2389 4387 9336 2783
239 235 453 3458
182 534 654 765
4485 0721 1308 2759
46759 543 2345
I want to grep only the numbers that have 4 digits together, 4 times in a row (seperated by a space).
For example: 4556 4618 7843 8732
I am using: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})" test.txt
Which shows me:
4556 4618 7843 8732
4532 0861 1932 5122
5440 3173 8207 0451 67886
6011 2966 7184 4668
4485 0721 1308 2759
Using this there is an extra line that shouldn't appear, where there is a 5th set of numbers that has 5 digits on the end.
So I used: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
But this only gave me two results instead of the 4 it should:
4556 4618 7843 8732
4485 0721 1308 2759
Can someone tell me what I'm doing wrong?
linux grep
So I have a text file:
4556 4618 7843 8732
4532 0861 1932 5122
3478 893* 6788 6312
5440 3173 8207 0451 67886
6011 2966 7184 4668
3678 3905 5323
2389 4387 9336 2783
239 235 453 3458
182 534 654 765
4485 0721 1308 2759
46759 543 2345
I want to grep only the numbers that have 4 digits together, 4 times in a row (seperated by a space).
For example: 4556 4618 7843 8732
I am using: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})" test.txt
Which shows me:
4556 4618 7843 8732
4532 0861 1932 5122
5440 3173 8207 0451 67886
6011 2966 7184 4668
4485 0721 1308 2759
Using this there is an extra line that shouldn't appear, where there is a 5th set of numbers that has 5 digits on the end.
So I used: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
But this only gave me two results instead of the 4 it should:
4556 4618 7843 8732
4485 0721 1308 2759
Can someone tell me what I'm doing wrong?
linux grep
linux grep
asked 3 hours ago
ESuth
82
82
You also appear to be missing2389 4387 9336 2783
-- are extra spaces the eliminating factor?
– glenn jackman
37 mins ago
add a comment |
You also appear to be missing2389 4387 9336 2783
-- are extra spaces the eliminating factor?
– glenn jackman
37 mins ago
You also appear to be missing
2389 4387 9336 2783
-- are extra spaces the eliminating factor?– glenn jackman
37 mins ago
You also appear to be missing
2389 4387 9336 2783
-- are extra spaces the eliminating factor?– glenn jackman
37 mins ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
$ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
4556 4618 7843 8732
4532 0861 1932 5122
6011 2966 7184 4668
4485 0721 1308 2759
Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.
The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.
As an alternative to using the ^
and $
anchors, you could instead use grep -x
:
grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'
And shortening this, just like Jeff has shown,
grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
3 hours ago
add a comment |
up vote
3
down vote
You got halfway there with the end-of-line anchor $
; you just need to anchor the beginning of the line, too, with ^
. Looks like you're OK with a leading space, so allow for that as well, with *
:
grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:
grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"
... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.
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',
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%2f487831%2fgrep-showing-extra-lines%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
up vote
3
down vote
accepted
$ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
4556 4618 7843 8732
4532 0861 1932 5122
6011 2966 7184 4668
4485 0721 1308 2759
Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.
The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.
As an alternative to using the ^
and $
anchors, you could instead use grep -x
:
grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'
And shortening this, just like Jeff has shown,
grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
3 hours ago
add a comment |
up vote
3
down vote
accepted
$ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
4556 4618 7843 8732
4532 0861 1932 5122
6011 2966 7184 4668
4485 0721 1308 2759
Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.
The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.
As an alternative to using the ^
and $
anchors, you could instead use grep -x
:
grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'
And shortening this, just like Jeff has shown,
grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
3 hours ago
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
$ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
4556 4618 7843 8732
4532 0861 1932 5122
6011 2966 7184 4668
4485 0721 1308 2759
Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.
The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.
As an alternative to using the ^
and $
anchors, you could instead use grep -x
:
grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'
And shortening this, just like Jeff has shown,
grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'
$ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
4556 4618 7843 8732
4532 0861 1932 5122
6011 2966 7184 4668
4485 0721 1308 2759
Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.
The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.
As an alternative to using the ^
and $
anchors, you could instead use grep -x
:
grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'
And shortening this, just like Jeff has shown,
grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'
edited 2 hours ago
answered 3 hours ago
Kusalananda
120k16225367
120k16225367
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
3 hours ago
add a comment |
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
3 hours ago
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
3 hours ago
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
3 hours ago
add a comment |
up vote
3
down vote
You got halfway there with the end-of-line anchor $
; you just need to anchor the beginning of the line, too, with ^
. Looks like you're OK with a leading space, so allow for that as well, with *
:
grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:
grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"
... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.
add a comment |
up vote
3
down vote
You got halfway there with the end-of-line anchor $
; you just need to anchor the beginning of the line, too, with ^
. Looks like you're OK with a leading space, so allow for that as well, with *
:
grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:
grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"
... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.
add a comment |
up vote
3
down vote
up vote
3
down vote
You got halfway there with the end-of-line anchor $
; you just need to anchor the beginning of the line, too, with ^
. Looks like you're OK with a leading space, so allow for that as well, with *
:
grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:
grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"
... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.
You got halfway there with the end-of-line anchor $
; you just need to anchor the beginning of the line, too, with ^
. Looks like you're OK with a leading space, so allow for that as well, with *
:
grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt
If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:
grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"
... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.
answered 3 hours ago
Jeff Schaller
37.8k1053122
37.8k1053122
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%2f487831%2fgrep-showing-extra-lines%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
You also appear to be missing
2389 4387 9336 2783
-- are extra spaces the eliminating factor?– glenn jackman
37 mins ago