I have script that greps some file and prints value , but it comes vertically like below
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
I want this should print as below
size date repo-name repo-path
190000 1603278566981 testupload /home/test/testupload
140000 1603278566981 testupload2 /home/test/testupload2
170000 1603278566981 testupload3 /home/test/testupload3
I tried things like below but it doesn't work
anyway I can print it horizontal with formatted way like below
size date repo-name repo-path
190000 1603278566981 testupload /home/test/testupload
140000 1603278566981 testupload2 /home/test/testupload2
170000 1603278566981 testupload3 /home/test/testupload3
Please suggest and help
Could you please try following, written and tested with shown samples in 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
Explanation: Adding detailed explanation for above.
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.