Grep Showing Extra Lines
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 |
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
Dec 13 at 22:00
add a comment |
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 Dec 13 at 18:43
ESuth
82
82
You also appear to be missing2389 4387 9336 2783
-- are extra spaces the eliminating factor?
– glenn jackman
Dec 13 at 22:00
add a comment |
You also appear to be missing2389 4387 9336 2783
-- are extra spaces the eliminating factor?
– glenn jackman
Dec 13 at 22:00
You also appear to be missing
2389 4387 9336 2783
-- are extra spaces the eliminating factor?– glenn jackman
Dec 13 at 22:00
You also appear to be missing
2389 4387 9336 2783
-- are extra spaces the eliminating factor?– glenn jackman
Dec 13 at 22:00
add a comment |
2 Answers
2
active
oldest
votes
$ 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
Dec 13 at 18:51
add a comment |
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',
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%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
$ 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
Dec 13 at 18:51
add a comment |
$ 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
Dec 13 at 18:51
add a comment |
$ 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 Dec 13 at 19:50
answered Dec 13 at 18:48
Kusalananda
121k16227372
121k16227372
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
Dec 13 at 18:51
add a comment |
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
Dec 13 at 18:51
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
Dec 13 at 18:51
This works great, exactly what I was looking for. I was forgetting to use *
– ESuth
Dec 13 at 18:51
add a comment |
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 |
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 |
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 Dec 13 at 18:47
Jeff Schaller
38.3k1053125
38.3k1053125
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
Dec 13 at 22:00