0

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.

  1. 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
    
  2. 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

3
$ 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.

0

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 '18 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 '18 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 '18 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 '18 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 '18 at 6:50
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.