Print only the lines that with all digits except the last one or the last two characters or the first or...
up vote
0
down vote
favorite
I have a big text file has lines with all numbers and with all letters and characters and also has lines mixed with numbers , letters and characters i want to print only the lines that have all numbers except the last or the last two characters.
Print the line that start with number but ends with any character at the last or the last two characters that is not a digit.
For example
1234567a
2245678902da
A1234566d
12345678abc
The output have to be
1234567a
22345678902da
Print the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
For example
A1234
Ab1234
1a1234
Abc1234
The output have to be
A1234
Ab1234
Thanks
sed command-line
New contributor
add a comment |
up vote
0
down vote
favorite
I have a big text file has lines with all numbers and with all letters and characters and also has lines mixed with numbers , letters and characters i want to print only the lines that have all numbers except the last or the last two characters.
Print the line that start with number but ends with any character at the last or the last two characters that is not a digit.
For example
1234567a
2245678902da
A1234566d
12345678abc
The output have to be
1234567a
22345678902da
Print the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
For example
A1234
Ab1234
1a1234
Abc1234
The output have to be
A1234
Ab1234
Thanks
sed command-line
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a big text file has lines with all numbers and with all letters and characters and also has lines mixed with numbers , letters and characters i want to print only the lines that have all numbers except the last or the last two characters.
Print the line that start with number but ends with any character at the last or the last two characters that is not a digit.
For example
1234567a
2245678902da
A1234566d
12345678abc
The output have to be
1234567a
22345678902da
Print the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
For example
A1234
Ab1234
1a1234
Abc1234
The output have to be
A1234
Ab1234
Thanks
sed command-line
New contributor
I have a big text file has lines with all numbers and with all letters and characters and also has lines mixed with numbers , letters and characters i want to print only the lines that have all numbers except the last or the last two characters.
Print the line that start with number but ends with any character at the last or the last two characters that is not a digit.
For example
1234567a
2245678902da
A1234566d
12345678abc
The output have to be
1234567a
22345678902da
Print the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
For example
A1234
Ab1234
1a1234
Abc1234
The output have to be
A1234
Ab1234
Thanks
sed command-line
sed command-line
New contributor
New contributor
edited yesterday
Kusalananda
120k16225369
120k16225369
New contributor
asked yesterday
Crun
12
12
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
$ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
1234567a
2245678902da
The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2})
will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x
option to grep
ensures that the match will be across full lines.
Note that this literal interpretation of what you specified also matches lines that contain only digits.
With
$ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
A1234
Ab1234
we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.
For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed
):
The first:
$ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
(234567)(a)
(2245678902)(da)
The second:
$ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
(A)(1234)
(Ab)(1234)
Remove all parentheses from the sed
command to get the sed
solution.
add a comment |
up vote
0
down vote
Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get
grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
1234567a
2245678902da
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
yesterday
@Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
– RudiC
yesterday
@Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
– Crun
yesterday
@Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
– Kusalananda
yesterday
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
23 hours 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',
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
});
}
});
Crun is a new contributor. Be nice, and check out our Code of Conduct.
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%2f489181%2fprint-only-the-lines-that-with-all-digits-except-the-last-one-or-the-last-two-ch%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
$ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
1234567a
2245678902da
The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2})
will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x
option to grep
ensures that the match will be across full lines.
Note that this literal interpretation of what you specified also matches lines that contain only digits.
With
$ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
A1234
Ab1234
we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.
For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed
):
The first:
$ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
(234567)(a)
(2245678902)(da)
The second:
$ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
(A)(1234)
(Ab)(1234)
Remove all parentheses from the sed
command to get the sed
solution.
add a comment |
up vote
3
down vote
$ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
1234567a
2245678902da
The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2})
will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x
option to grep
ensures that the match will be across full lines.
Note that this literal interpretation of what you specified also matches lines that contain only digits.
With
$ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
A1234
Ab1234
we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.
For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed
):
The first:
$ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
(234567)(a)
(2245678902)(da)
The second:
$ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
(A)(1234)
(Ab)(1234)
Remove all parentheses from the sed
command to get the sed
solution.
add a comment |
up vote
3
down vote
up vote
3
down vote
$ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
1234567a
2245678902da
The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2})
will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x
option to grep
ensures that the match will be across full lines.
Note that this literal interpretation of what you specified also matches lines that contain only digits.
With
$ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
A1234
Ab1234
we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.
For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed
):
The first:
$ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
(234567)(a)
(2245678902)(da)
The second:
$ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
(A)(1234)
(Ab)(1234)
Remove all parentheses from the sed
command to get the sed
solution.
$ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
1234567a
2245678902da
The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2})
will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x
option to grep
ensures that the match will be across full lines.
Note that this literal interpretation of what you specified also matches lines that contain only digits.
With
$ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
A1234
Ab1234
we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.
For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed
):
The first:
$ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
(234567)(a)
(2245678902)(da)
The second:
$ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
(A)(1234)
(Ab)(1234)
Remove all parentheses from the sed
command to get the sed
solution.
edited yesterday
answered yesterday
Kusalananda
120k16225369
120k16225369
add a comment |
add a comment |
up vote
0
down vote
Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get
grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
1234567a
2245678902da
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
yesterday
@Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
– RudiC
yesterday
@Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
– Crun
yesterday
@Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
– Kusalananda
yesterday
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
23 hours ago
add a comment |
up vote
0
down vote
Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get
grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
1234567a
2245678902da
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
yesterday
@Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
– RudiC
yesterday
@Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
– Crun
yesterday
@Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
– Kusalananda
yesterday
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
23 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get
grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
1234567a
2245678902da
Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get
grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
1234567a
2245678902da
answered yesterday
Doug O'Neal
2,8321817
2,8321817
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
yesterday
@Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
– RudiC
yesterday
@Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
– Crun
yesterday
@Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
– Kusalananda
yesterday
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
23 hours ago
add a comment |
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
yesterday
@Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
– RudiC
yesterday
@Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
– Crun
yesterday
@Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
– Kusalananda
yesterday
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
23 hours ago
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
yesterday
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
yesterday
@Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
– RudiC
yesterday
@Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
– RudiC
yesterday
@Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
– Crun
yesterday
@Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
– Crun
yesterday
@Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
– Kusalananda
yesterday
@Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
– Kusalananda
yesterday
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
23 hours ago
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
23 hours ago
add a comment |
Crun is a new contributor. Be nice, and check out our Code of Conduct.
Crun is a new contributor. Be nice, and check out our Code of Conduct.
Crun is a new contributor. Be nice, and check out our Code of Conduct.
Crun is a new contributor. Be nice, and check out our Code of Conduct.
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%2f489181%2fprint-only-the-lines-that-with-all-digits-except-the-last-one-or-the-last-two-ch%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