我的脚本可以抓取一些文件并输出值,但是它像下面这样垂直出现
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
我尝试了以下类似的操作,但不起作用无论如何,我可以用下面的格式化方式将其水平打印
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.
关于linux - 如何在Linux中水平打印awk输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/65068627/