awk在字符类中使用空格

时间:2014-08-11 20:07:37

标签: awk

有人能告诉我如何使用awk根据字符类解析列中的空格吗?我有一些随意的输入,我试图使用以下方法解析它:

somecommand | awk -F'[:space:]' '{print $2}' 

我也试过

somecommand | awk -F'[ ]' '{print $2}' 

somecommand | awk -F'[[:space:]]' '{print $2}'

我确信下面的命令有效,虽然我无法使用它,因为我试图解析多个分隔符,空格是其中之一(冒号是另一个),这就是为什么我&#39 ;我试图使用一个字符类。

 somecommand | awk -F' ' '{print $2}'

问题是我在前三个例子的输出中得到了空格,我显然不会期望。我确信我实际上在输出中得到了空格,因为我正在输出到" od -a"并且看到角色实际上是一个空间。我正在使用GNU Awk 3.1.7。感谢您的帮助: - )

]$ cat hi.txt | awk -F'[[:blank:]]' '{print $2,$3}' 
 *
 *
 *
 *
 *
]$ cat hi.txt
  * PID: 4240    Sessions: 0       Processed: 1       Uptime: 3m 28,    URL     : http://127.0.0.1:56784, Password: F3N04418IlVe9230Vs2bnJ7lPk9KE2PIzhlSOrv173X
  * PID: 4247    Sessions: 0       Processed: 0       Uptime: 3m 28,    URL     : http://127.0.0.1:59918, Password: g0jOTdEawcbFluPMGawbbeSo3u7mQvYlT5P136ymELh
  * PID: 4254    Sessions: 0       Processed: 0       Uptime: 3m 28,    URL     : http://127.0.0.1:60870, Password: C9QHDrxfCPZjZd1AiIvbSdnaG3rI0QuvfUf4stvdqww
  * PID: 4261    Sessions: 0       Processed: 0       Uptime: 3m 28,    URL     : http://127.0.0.1:56414, Password: cnPMFUXq6f6AtkcqOCqba7Bx76mC3K7uX7XxCfIDYYq
  * PID: 4268    Sessions: 0       Processed: 0       Uptime: 3m 28,    URL     : http://127.0.0.1:53253, Password: yAff1NmrTz4B6kUKCsZ0HPvA75bWAvmOl8YHUi9aOgo
]$ awk --version
GNU Awk 3.1.7
Copyright (C) 1989, 1991-2009 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
~]$ cat hi.txt | od -a
0000000  sp  sp   *  sp   P   I   D   :  sp   4   2   4   0  sp  sp  sp
0000020  sp   S   e   s   s   i   o   n   s   :  sp   0  sp  sp  sp  sp
0000040  sp  sp  sp   P   r   o   c   e   s   s   e   d   :  sp   1  sp
0000060  sp  sp  sp  sp  sp  sp   U   p   t   i   m   e   :  sp   3   m
0000100  sp   2   8   ,  sp  sp  sp  sp   U   R   L  sp  sp  sp  sp  sp
0000120   :  sp   h   t   t   p   :   /   /   1   2   7   .   0   .   0
0000140   .   1   :   5   6   7   8   4   ,  sp   P   a   s   s   w   o
0000160   r   d   :  sp   F   3   N   0   4   4   1   8   I   l   V   e
0000200   9   2   3   0   V   s   2   b   n   J   7   l   P   k   9   K
0000220   E   2   P   I   z   h   l   S   O   r   v   1   7   3   X  nl
0000240  sp  sp   *  sp   P   I   D   :  sp   4   2   4   7  sp  sp  sp
0000260  sp   S   e   s   s   i   o   n   s   :  sp   0  sp  sp  sp  sp
0000300  sp  sp  sp   P   r   o   c   e   s   s   e   d   :  sp   0  sp
0000320  sp  sp  sp  sp  sp  sp   U   p   t   i   m   e   :  sp   3   m
0000340  sp   2   8   ,  sp  sp  sp  sp   U   R   L  sp  sp  sp  sp  sp
0000360   :  sp   h   t   t   p   :   /   /   1   2   7   .   0   .   0
0000400   .   1   :   5   9   9   1   8   ,  sp   P   a   s   s   w   o
0000420   r   d   :  sp   g   0   j   O   T   d   E   a   w   c   b   F
0000440   l   u   P   M   G   a   w   b   b   e   S   o   3   u   7   m
0000460   Q   v   Y   l   T   5   P   1   3   6   y   m   E   L   h  nl
0000500  sp  sp   *  sp   P   I   D   :  sp   4   2   5   4  sp  sp  sp
0000520  sp   S   e   s   s   i   o   n   s   :  sp   0  sp  sp  sp  sp
0000540  sp  sp  sp   P   r   o   c   e   s   s   e   d   :  sp   0  sp
0000560  sp  sp  sp  sp  sp  sp   U   p   t   i   m   e   :  sp   3   m
0000600  sp   2   8   ,  sp  sp  sp  sp   U   R   L  sp  sp  sp  sp  sp
0000620   :  sp   h   t   t   p   :   /   /   1   2   7   .   0   .   0
0000640   .   1   :   6   0   8   7   0   ,  sp   P   a   s   s   w   o
0000660   r   d   :  sp   C   9   Q   H   D   r   x   f   C   P   Z   j
0000700   Z   d   1   A   i   I   v   b   S   d   n   a   G   3   r   I
0000720   0   Q   u   v   f   U   f   4   s   t   v   d   q   w   w  nl
0000740  sp  sp   *  sp   P   I   D   :  sp   4   2   6   1  sp  sp  sp
0000760  sp   S   e   s   s   i   o   n   s   :  sp   0  sp  sp  sp  sp
0001000  sp  sp  sp   P   r   o   c   e   s   s   e   d   :  sp   0  sp
0001020  sp  sp  sp  sp  sp  sp   U   p   t   i   m   e   :  sp   3   m
0001040  sp   2   8   ,  sp  sp  sp  sp   U   R   L  sp  sp  sp  sp  sp
0001060   :  sp   h   t   t   p   :   /   /   1   2   7   .   0   .   0
0001100   .   1   :   5   6   4   1   4   ,  sp   P   a   s   s   w   o
0001120   r   d   :  sp   c   n   P   M   F   U   X   q   6   f   6   A
0001140   t   k   c   q   O   C   q   b   a   7   B   x   7   6   m   C
0001160   3   K   7   u   X   7   X   x   C   f   I   D   Y   Y   q  nl
0001200  sp  sp   *  sp   P   I   D   :  sp   4   2   6   8  sp  sp  sp
0001220  sp   S   e   s   s   i   o   n   s   :  sp   0  sp  sp  sp  sp
0001240  sp  sp  sp   P   r   o   c   e   s   s   e   d   :  sp   0  sp
0001260  sp  sp  sp  sp  sp  sp   U   p   t   i   m   e   :  sp   3   m
0001300  sp   2   8   ,  sp  sp  sp  sp   U   R   L  sp  sp  sp  sp  sp
0001320   :  sp   h   t   t   p   :   /   /   1   2   7   .   0   .   0
0001340   .   1   :   5   3   2   5   3   ,  sp   P   a   s   s   w   o
0001360   r   d   :  sp   y   A   f   f   1   N   m   r   T   z   4   B
0001400   6   k   U   K   C   s   Z   0   H   P   v   A   7   5   b   W
0001420   A   v   m   O   l   8   Y   H   U   i   9   a   O   g   o  nl
0001440
[a.cri.dsullivan@aaa-arnor04 ~]$ cat hi.txt | awk -F'[[:blank:]]' '{print $4,$6}' 
PID: 
PID: 
PID: 
PID: 
PID: 
[a.cri.dsullivan@aaa-arnor04 ~]$ cat hi.txt | awk -F'[[:blank:]]' '{print $4,$7}' 
PID: 
PID: 
PID: 
PID: 
PID: 
[a.cri.dsullivan@aaa-arnor04 ~]$ cat hi.txt | awk -F'[[:space:]]' '{print $4,$7}' 
PID: 
PID: 
PID: 
PID: 
PID: 

2 个答案:

答案 0 :(得分:4)

这是一个带分号,逗号和冒号的空格类:

[[:space:];:,]

以及其他一些变化:

[[:blank:][:cntrl:]]
[ \t,:;]

更新

试试这个:

somecommand | awk -F'[[:space:]]+' '{print $2}'  ## + or *, they're probably just similar.

答案 1 :(得分:0)

有几个不同的Field Separator值可能会执行您想要的操作:

FS="[[:space:]:]+"

FS="[[:space:]]+|:"

FS="[[:space:]]*:[[:space:]]+"

或其他。鉴于您在https://gist.github.com/anonymous/a007032c9facd9e71dff发布的输入数据,最后一个实际上看起来就像您真正想要的那样。

如果您使用FS="[[:space:]:]+",它会将您的网址值(例如http://127.0.0.1:56784)分成:周围的3个单独部分,其他选项会对各个字段产生类似的负面影响