Print only the lines that with all digits except the last one or the last two characters or the first or second characters

Clash Royale CLAN TAG#URR8PPP
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 example1234567a
2245678902da
A1234566d
12345678abcThe output have to be
1234567a
22345678902daPrint the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
For exampleA1234
Ab1234
1a1234
Abc1234The output have to be
A1234
Ab1234
Thanks
sed command-line
add a comment |
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 example1234567a
2245678902da
A1234566d
12345678abcThe output have to be
1234567a
22345678902daPrint the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
For exampleA1234
Ab1234
1a1234
Abc1234The output have to be
A1234
Ab1234
Thanks
sed command-line
add a comment |
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 example1234567a
2245678902da
A1234566d
12345678abcThe output have to be
1234567a
22345678902daPrint the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
For exampleA1234
Ab1234
1a1234
Abc1234The output have to be
A1234
Ab1234
Thanks
sed command-line
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 example1234567a
2245678902da
A1234566d
12345678abcThe output have to be
1234567a
22345678902daPrint the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
For exampleA1234
Ab1234
1a1234
Abc1234The output have to be
A1234
Ab1234
Thanks
sed command-line
sed command-line
edited Dec 15 at 21:04
Kusalananda
121k16229372
121k16229372
asked Dec 15 at 18:19
Crun
12
12
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
$ 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 |
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
Dec 15 at 18:48
@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
Dec 15 at 18:56
@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
Dec 15 at 20:36
@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
Dec 15 at 21:03
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
Dec 16 at 6:50
|
show 1 more comment
For first question below is command
sed -n '/^[0-9]*[a-z],2$/p' filename
output
1234567a
2245678902da
For second question below is the command
sed -n '/^[A-Za-z]1,2[0-9]*$/p'
output:
A1234
Ab1234
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%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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$ 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 |
$ 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 |
$ 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 Dec 15 at 21:14
answered Dec 15 at 18:27
Kusalananda
121k16229372
121k16229372
add a comment |
add a comment |
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
Dec 15 at 18:48
@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
Dec 15 at 18:56
@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
Dec 15 at 20:36
@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
Dec 15 at 21:03
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
Dec 16 at 6:50
|
show 1 more comment
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
Dec 15 at 18:48
@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
Dec 15 at 18:56
@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
Dec 15 at 20:36
@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
Dec 15 at 21:03
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
Dec 16 at 6:50
|
show 1 more comment
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 Dec 15 at 18:30
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
Dec 15 at 18:48
@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
Dec 15 at 18:56
@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
Dec 15 at 20:36
@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
Dec 15 at 21:03
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
Dec 16 at 6:50
|
show 1 more comment
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
Dec 15 at 18:48
@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
Dec 15 at 18:56
@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
Dec 15 at 20:36
@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
Dec 15 at 21:03
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
Dec 16 at 6:50
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
Dec 15 at 18:48
Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
– Crun
Dec 15 at 18:48
@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
Dec 15 at 18:56
@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
Dec 15 at 18:56
@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
Dec 15 at 20:36
@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
Dec 15 at 20:36
@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
Dec 15 at 21:03
@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
Dec 15 at 21:03
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
Dec 16 at 6:50
@Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
– Crun
Dec 16 at 6:50
|
show 1 more comment
For first question below is command
sed -n '/^[0-9]*[a-z],2$/p' filename
output
1234567a
2245678902da
For second question below is the command
sed -n '/^[A-Za-z]1,2[0-9]*$/p'
output:
A1234
Ab1234
add a comment |
For first question below is command
sed -n '/^[0-9]*[a-z],2$/p' filename
output
1234567a
2245678902da
For second question below is the command
sed -n '/^[A-Za-z]1,2[0-9]*$/p'
output:
A1234
Ab1234
add a comment |
For first question below is command
sed -n '/^[0-9]*[a-z],2$/p' filename
output
1234567a
2245678902da
For second question below is the command
sed -n '/^[A-Za-z]1,2[0-9]*$/p'
output:
A1234
Ab1234
For first question below is command
sed -n '/^[0-9]*[a-z],2$/p' filename
output
1234567a
2245678902da
For second question below is the command
sed -n '/^[A-Za-z]1,2[0-9]*$/p'
output:
A1234
Ab1234
answered Dec 18 at 14:17
Praveen Kumar BS
1,186138
1,186138
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%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