Warm tip: This article is reproduced from serverfault.com, please click

bash-如何在Linux中水平打印awk输出

(bash - how to print awk output horizontal in linux)

发布于 2020-11-30 05:43:52

我的脚本可以抓取一些文件并输出值,但是它像下面这样垂直出现

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

请提出建议和帮助

Questioner
Samurai
Viewed
11
RavinderSingh13 2020-11-30 14:21:14

你可以尝试按照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.