繁体   English   中英

在Solaris上的awk中使用多字符字段分隔符

[英]Using a multi-character field separator in awk on Solaris

我希望在awk使用字符串(BIRCH)作为字段定界符以打印第二个字段。 我正在尝试以下命令:

cat tmp.log|awk -FBirch '{ print $2}'

下面的输出正在打印:

irch2014 / 06 / 23,04:36:45,3,1401503,XML-哈伦,P12345-1,温度,0a653356353635635,温度,L,成功

所需的输出:

2014/06 / 23,04:36:45,3,1401503,XML-哈伦,P12345-1,温度,0a653356353635635,温度,L,成功

tmp.log文件的内容。

-bash-3.2# cat tmp.log  
Dec 05 13:49:23 [x.x.x.x.180.100] business-log-dev/int [TEST][0x80000001][business-log][info] mpgw(Test): trans(8497187)[request][10.x.x.x]:
Birch2014/06/23,04:36:45,3,1401503,xml-harlan,P12345-1,temp,0a653356353635635,temp,L,Success

难道我做错了什么?

  • 操作系统:Solaris10
  • 外壳:重击

在下面的回答之一中建议使用以下命令进行尝试。 我正在获得所需的输出,但顶部有一个额外的空行。 如何从输出中消除呢?

-bash-3.2# /usr/xpg4/bin/awk -FBirch '{print $2}' tmp.log

2014/06/23,04:36:45,3,1401503,xml-harlan,P12345-1,temp,0a653356353635635,temp,L,Success

最初,我建议在“ Birch”(- -F'Birch' )周围-F'Birch'引号,但实际上,我认为这没有什么区别。

我对使用Solaris没有任何经验,但是您可能还想尝试使用nawk (“ new awk”)而不是awk

nawk -FBirch '{print $2}' file

如果可行,则可能需要考虑创建别名,以便始终使用具有更多功能的较新版本的awk。

您可能还想尝试在/usr/xpg4/bin目录中使用awk的版本,它是POSIX兼容的实现,因此应支持多字符FS

/usr/xpg4/bin/awk -FBirch '{print $2}' file

如果只想打印具有多个字段的行,则可以添加条件:

/usr/xpg4/bin/awk -FBirch 'NF>1{print $2}' file

仅当有多个字段时,才打印第二个字段。

在solaris usr/bin/awk上的默认awk的手册页中

-Fc            Uses the character c as the  field  separator
               (FS)  character.   See  the  discussion of FS
               below.

如您所见,solaris awk只需要一个字符作为字段分隔符

也在手册页中拆分

split(s, a, fs)

     Split the string s into array elements a[1],  a[2],  ...
     a[n],  and  returns  n.  The separation is done with the
     regular expression fs or with the field separator FS  if
     fs is not given.

如您所见,它使用正则表达式作为分隔符,因此我们可以使用它。

awk 'split($0,a,"Birch"){print a[2]}' file

打印由Birch分割的第二个字段

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM