I am trying to get the lines of held emails with grep or egrep that contain an exclamation point / bang

root@server:~# mailq

-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
0528561D88      878 Wed Feb  1 21:46:12  [email protected]
                                         [email protected]

0D14161E2B      657 Wed Feb  x xx:47:01  [email protected]
                                         [email protected]

0798C61E0F      657 Wed Feb  x xx:45:02  [email protected]
                                         [email protected]

14AF361E2F!     657 Wed Feb  x xx:48:01  [email protected]
                                         [email protected]


root@server:~# mailq |grep "[[:alnum:]]\!"
3658861E66!     657 Wed Feb  x xx:48:01  [email protected]
root@server:~# mailq |grep "^[[:alnum:]]\!"

The first grep works provides the expected result, but the second one does not work at all

Any thoughts?

  • 3
    [[:alnum:]] matches precisely one character. How many characters are there between the beginning of the line and the bang?
    – thrig
    Feb 1, 2017 at 20:13
  • @don_crissti the expected print is 3658861E66! 657 Wed Feb x xx:48:01 [email protected]
    – marios88
    Feb 1, 2017 at 20:19
  • @thrig 10 characters
    – marios88
    Feb 1, 2017 at 20:20

2 Answers 2


The second regular expression, ^[[:alnum:]]! matches a single alphanumeric character at the start of the line, followed by an exclamation point.

For example


But not


(ten alphanumeric character, and !)

To match exactly ten characters and !, use

$ mailq | grep -E '^[[:alnum:]]{10}!'

You are grepping for one alnum character followed by a bang. Try the pattern ^[[:alnum:]]{10}! instead.

  • grep did not work but egrep did the job egrep "^[[:alnum:]]{10}!"
    – marios88
    Feb 1, 2017 at 20:32
  • just tried out what you suggested out of curiosity but apparently it didnt work root@server:~# mailq | egrep "^[A-Za-z0-9]{10}!" ED36761E87! 657 Wed Feb x xx:40:01 [email protected] root@server:~# mailq | grep "^[A-Za-z0-9]{10}!" root@server:~#
    – marios88
    Feb 1, 2017 at 20:42
  • My mistake. I shall remove my erroneous comment.
    – DopeGhoti
    Feb 1, 2017 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .