繁体   English   中英

如何在 linux 中水平打印 awk output

[英]how to print awk output horizontal in linux

我有一些脚本可以greps一些文件并打印值,但它是垂直的,如下所示

size=190000
date=1603278566981
repo-name=testupload
repo-path=/home/test/testupload
size=140000
date=1603278566981
repo-name=testupload2
repo-path=/home/test/testupload2
size=170000
date=1603278566981
repo-name=testupload3
repo-path=/home/test/testupload3

我希望这应该打印如下

size    date            repo-name          repo-path
190000  1603278566981   testupload      /home/test/testupload
140000  1603278566981   testupload2     /home/test/testupload2
170000  1603278566981   testupload3     /home/test/testupload3

我尝试了以下类似的方法,但它不起作用

awk

无论如何,我可以使用如下格式的方式将其水平打印

size    date            repo-name          repo-path
190000  1603278566981   testupload      /home/test/testupload
140000  1603278566981   testupload2     /home/test/testupload2
170000  1603278566981   testupload3     /home/test/testupload3

请建议和帮助

您能否尝试使用 GNU awk中的示例进行跟踪、编写和测试。

awk '
BEGIN{ FS="=" }
/^size/{
  if(++count1==1){ header=$1 }
  sizeArr[++count]=$NF
  next
}
/^date/{
  if(++count2==1){ header=header OFS $1 }
  dateArr[count]=$NF
  next
}
/^repo-name/{
  if(++count3==1){ header=header OFS $1 }
  repoNameArr[count]=$NF
  next
}
/^repo-path/{
  if(++count4==1){ header=header OFS $1 }
  repopathArr[count]=$NF
  next
}
END{
  print header
  for(i=1;i<=count;i++){
    printf("%s %s %s %s\n",sizeArr[i],dateArr[i],repoNameArr[i],repopathArr[i])
  }
}
' Input_file | column -t

说明:为上述添加详细说明。

awk '                                        ##Starting awk program from here.
BEGIN{ FS="=" }                              ##Starting BEGIN section from here and setting field separator as = here.
/^size/{                                     ##If line starts from size then do following.
  if(++count1==1){ header=$1 }               ##Checking if count1 variable is 1 then setting 1st field value as header.
  sizeArr[++count]=$NF                       ##Creating sizeArr with increasing count with 1 as an index and value is last field.
  next                                       ##next will skip all further statements.
}
/^date/{                                     ##If line starts from date then do
  if(++count2==1){ header=header OFS $1 }    ##Checking if count2 variable is 1 then setting 1st field value as header.
  dateArr[count]=$NF                         ##Creating dateArr with count as an index and value is last field.
  next                                       ##next will skip all further statements.
}
/^repo-name/{                                ##If line starts from repo-name then do
  if(++count3==1){ header=header OFS $1 }    ##Checking if count3 variable is 1 then setting 1st field value as header.
  repoNameArr[count]=$NF                     ##Creating repoNameArr with count as an index and value is last field.
  next                                       ##next will skip all further statements.
}
/^repo-path/{                                ##If line starts from repo-path then do
  if(++count4==1){ header=header OFS $1 }    ##Checking if count4 variable is 1 then setting 1st field value as header.
  repopathArr[count]=$NF                     ##Creating repopathArr with count as an index and value is last field.
  next                                       ##next will skip all further statements.
}
END{                                         ##Starting END block of this program from here.
  print header                               ##Printing header here.
  for(i=1;i<=count;i++){                     ##Starting loop from 1 to value of count.
    printf("%s %s %s %s\n",sizeArr[i],dateArr[i],repoNameArr[i],repopathArr[i]) ##Printing all array values here with index as i here.
  }
}
' Input_file | column -t                     ##mentioning Input_file name and sendig awk output to column command for better looks.

假设输入字段序列是恒定的,这里是 mawk 版本 1.3.4 的版本:

awk -v RS='size=' -v OFS='\t' '
BEGIN{ fmt = "%s" OFS "%s" OFS "%s" OFS" %s" ORS }
function h(s) { return substr(s,1,index(s,"=")-1) }
function v(s) { return substr(s,1+index(s,"=")) }
NF{ if (!hdr++)
        printf(fmt,h(RS),h($2),h($3),h($4))
    printf(fmt,$1,v($2),v($3),v($4))
}
' data | column -t

...虽然使用RS='(^|\n)size='并相应地编辑第一个 header 属性更安全。

Output:

size    date           repo-name    repo-path
190000  1603278566981  testupload   /home/test/testupload
140000  1603278566981  testupload2  /home/test/testupload2
170000  1603278566981  testupload3  /home/test/testupload3

暂无
暂无

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

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