我有一个details.txt文件,它包含以下数据
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
以及下面的awk脚本处理,即
#!/bin/bash
awk -vOFS='t' '
BEGIN{ FS="=" }
/^size/{
if(++count1==1){ header=$1"," }
sizeArr[++count]=$NF
next
}
/^@repo-name/{
if(++count2==1){ header=header OFS $1"," }
repoNameArr[count]=$NF
next
}
/^date/{
if(++count3==1){ header=header OFS $1"," }
dateArr[count]=$NF
next
}
/^@blob-name/{
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,%sn",sizeArr[i],repoNameArr[i],dateArr[i],repopathArr[i])
}
}
' details.txt | tr -d @ |awk -F, '{$3=substr($3,0,10)}1' OFS=,|sed 's/date/creationTime/g'
按预期打印值,(因为它有reponame(
size " repo-name" " creationTime" " blob-name"
10496000 testupload Fri 11 Dec 2020 07:35:56 AM CET testfile.tar11.gz
10496000 testupload Thu 10 Dec 2020 02:44:04 PM CET testfile.tar.gz
9602303 testupload Fri 11 Dec 2020 07:38:58 AM CET apache-maven-3.6.3-bin/apache-maven-3.6.3-bin.zip
但是,当文件格式中缺少某些内容时,文件的格式会出错(这里的reponame会跳到最后一列的标题,因为前几个数据没有reponame值(
size " creationTimeime" " blob-name" " " repo-name"
261304 Thu 13 Feb 2020 08:50:02 AM CET temp 8963d25231b
29639 Thu 13 Feb 2020 08:50:00 AM CET temp 3780c72cab5
93699 Thu 13 Feb 2020 08:50:00 AM CET temp 209276c91ba
列标题打印错误,但数据打印完美,如果其中一个字段不在,有什么可以验证的吗?它应该跳过它,以正确的格式打印其余字段。
如果数据不可用,则应保持该标头不变,而不应按标头顺序排列。
我的要求如果deatils.txt缺少任何记录,则应跳过该记录并打印为空白并按标题打印。如果repo名称字段不存在,但rest输出是正确的,则头会受到干扰,所以即使字段丢失,我们也需要保持头的完整性。
错误:尺寸";creationTimeime"斑点名称"quot;回购名称";261304 2020年2月13日星期四上午08:50:02欧洲中部时间温度8963d25231b29639 2020年2月13日星期四上午08:50:00欧洲中部时间温度3780c72cab593699 2020年2月13日星期四上午08:50:00欧洲中部时间温度209276c91ba
右侧
size " repo-name" " creationTime" " blob-name"
10496000 testupload Fri 11 Dec 2020 07:35:56 AM CET testfile.tar11.gz
10496000 testupload Thu 10 Dec 2020 02:44:04 PM CET testfile.tar.gz
9602303 testupload Fri 11 Dec 2020 07:38:58 AM CET apache-maven-3.6.3-bin/apache-maven-3.6.3-bin.zip
感谢
武士
您可以尝试此gnu awk
:
awk -F= -v OFS='t' 'function prt(ind, name, s) {s=map[ind][name]; return (s==""?" ":s);} {map[NR][$1] = $2} END {print "Size", "Repo Name", "CreationTime", "Repo Path"; for (i=1; i<=NR; i+=4) print prt(i, "size"), prt(i+2, "repo-name"), prt(i+1, "date"), prt(i+3, "repo-path")}' file
Size Repo Name CreationTime Repo Path
190000 testupload 1603278566981 /home/test/testupload
140000 testupload2 1603278566981 /home/test/testupload2
170000 testupload3 1603278566981 /home/test/testupload3
使其可读:
awk -F= -v OFS='t' 'function prt(ind, name, s) {
s = map[ind][name]
return (s==""?" ":s)
}
{
map[NR][$1] = $2
}
END {
print "Size", "Repo Name", "CreationTime", "Repo Path"
for (i=1; i<=NR; i+=4)
print prt(i, "size"), prt(i+2, "repo-name"), prt(i+1, "date"), prt(i+3, "repo-path")
}' file