我想提取我们在执行命令时得到的消息号mailx -H
。我只想要未读消息和新消息的消息编号。我尝试使用以下命令:
mailx -H|grep '^ [UN]'|cut -c 3-
但它没有给出所需的输出。它给出了 U 或 N 之后的整行。 mailx -H 命令的示例输出是:
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
我想要 U 或 N 后面的数字,即新消息或未读消息以及 O(旧)消息。如何在 shell 脚本中完成此操作?预期输出是
95
96
97
98
99
100
101
102
答案1
尝试这个,
mailx -H | nawk -F '[^0-9]+' '/^ [U|N]/ { print $2}'
[^0-9]+
作为一个FS。U
提取以or开头的行N
- 打印第二个字段
答案2
尝试这个grep
:
grep -P -o '(?<=O|U|N) ?[0-9]+'
例子:
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
如果grep
上述方法不起作用/不够,请尝试以下操作sed
:
sed -E 's/^(O|U|N) ?([0-9]+) .*/\2/g'
例子:
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