0

I want to extract message number which we get on executing command mailx -H. I want the message numbers of unread and new messages only. I tried with the below command:

mailx -H|grep '^ [UN]'|cut -c 3-

But it is not giving the required output.It gives the entire line following U or N. The sample output of mailx -H command is:

O 95 abcd Thu Sep  6 20:29   25/1245  Incident: 00291
O 96 efgh Thu Sep  6 20:29   25/1245  Incident: 00291
O 97 abcd  Thu Sep  6 20:29   25/1245 Incident: 00291
O 98 pqrs Thu Sep  6 20:29   25/1245  Incident: 00291
O 99 abcd  Thu Sep  6 20:29   25/1245 Incident: 00291
U100 cnhn Thu Sep  6 20:29   25/1244  Incident: 00291
U101 gont Thu Sep  6 20:29   25/1244  Incident: 00291
U102 qwer Thu Sep  6 20:29   25/1244  Incident: 00291

I want number following U or N ie new or unread messages and O(old) messages. How can this be done in a shell script? The expected output is

95
96
97
98
99
100
101
102
3
  • 1
    can you share the expected result and distribution of Linux.
    – Siva
    Sep 6 '18 at 11:19
  • updated it in the question
    – Devjith
    Sep 6 '18 at 11:21
  • 1
    Welcome , please add your grep version.
    – GAD3R
    Sep 6 '18 at 11:46
2

Try this,

mailx -H | nawk -F '[^0-9]+' '/^ [U|N]/ { print $2}' 
  • [^0-9]+ as an FS.
  • extract the lines which started with U or N
  • print second field
3
  • @Devjith are you using solaris?
    – Siva
    Sep 6 '18 at 11:55
  • @Devjith try with nawk
    – Siva
    Sep 6 '18 at 11:59
  • 3
    @Devjith plz mention the distro if your are asking any question in future.
    – Siva
    Sep 6 '18 at 12:04
1

Try this grep:

grep -P -o '(?<=O|U|N) ?[0-9]+'

Example:

echo "O 95 abcd Thu Sep  6 20:29   25/1245  Incident: 00291
O 96 efgh Thu Sep  6 20:29   25/1245  Incident: 00291
O 97 abcd  Thu Sep  6 20:29   25/1245 Incident: 00291
O 98 pqrs Thu Sep  6 20:29   25/1245  Incident: 00291
O 99 abcd  Thu Sep  6 20:29   25/1245 Incident: 00291
U100 cnhn Thu Sep  6 20:29   25/1244  Incident: 00291
U101 gont Thu Sep  6 20:29   25/1244  Incident: 00291
U102 qwer Thu Sep  6 20:29   25/1244  Incident: 00291" | grep -P -o '(?<=O|U|N) ?[0-9]+'
 95
 96
 97
 98
 99
100
101
102

-

grep --version
grep (GNU grep) 2.27

If the grep above does not work/suffice, try this sed:

sed -E 's/^(O|U|N) ?([0-9]+) .*/\2/g'

Example:

echo "O 95 abcd Thu Sep  6 20:29   25/1245  Incident: 00291
O 96 efgh Thu Sep  6 20:29   25/1245  Incident: 00291
O 97 abcd  Thu Sep  6 20:29   25/1245 Incident: 00291
O 98 pqrs Thu Sep  6 20:29   25/1245  Incident: 00291
O 99 abcd  Thu Sep  6 20:29   25/1245 Incident: 00291
U100 cnhn Thu Sep  6 20:29   25/1244  Incident: 00291
U101 gont Thu Sep  6 20:29   25/1244  Incident: 00291
U102 qwer Thu Sep  6 20:29   25/1244  Incident: 00291" | sed -E 's/^(O|U|N) ?([0-9]+) .*/\2/g'
95
96
97
98
99
100
101
102
7
  • I want the number alone. Also, grep -E -o is shown as an illegal option for me..
    – Devjith
    Sep 6 '18 at 11:16
  • Updated, see above. Sep 6 '18 at 11:26
  • grep: illegal option -- P grep: illegal option -- o Usage: grep -hblcnsviw pattern file . . . I am getting this error
    – Devjith
    Sep 6 '18 at 11:28
  • What GNU/Linux distribution (if any) are you using? Sep 6 '18 at 11:29
  • 2
    Well... since we don't know your grep version, your sed version, or which operating system you are using, it is hard to help... Sep 6 '18 at 12:00

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.