일부 파일을 greps하고 value를 인쇄하는 스크립트가 있지만 아래와 같이 세로로 제공됩니다.
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.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다