之前在做采集软件版本号的时候,碰到了如下格式判断Linux 输出每行的列数个数yum list installed输出格式如下,有些行只有一个名称,有些行只有版本号的情况,这样给我们带来了比较大的解析成本。

解决方法:因为每三个虽然岁在行不一样,其实内容的类型是一样的,所以我们用

 yum list installed |sed -n '3,$p'|awk '{a+=NF}END{print a}' 
统计下 输出结果2838刚好有这么多个行*列=2838能被3整除
所以我们可以判断下:读取每行的时候如果NF(列个数)为1我们就在a变量累加1,如果为2就累加2,当刚好加到3能被3整除的时候   我们就换行,具体如下请看代码:
 yum list installed |sed -n '3,$p'| awk '{if(NF==1){a++;printf $1"\t"}else if(NF==2){a+=2;printf $1"\t"$2"\t"}else if(NF==3){a+=3;printf $1"\t"$2"\t"$3}else{print "ERROR"}; if(a>0 && (a%3)==0){printf "\n"}}' 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-06-20
猜你喜欢
  • 2021-07-11
  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
相关资源
相似解决方案