更新时间:2023-10-03 14:15:40
自2D数组以来,这是GNU awk中的一个:
Here's one in GNU awk since using 2D arrays:
$ awk '
BEGIN {
FS=OFS="\t" } # set the delimiters
{
sub(/\r/,"",$NF) # in case of \r\n line endings
a[NR][1] # define array element
n=split($0,a[NR],FS) # split record to a[NR]
a[NR][4]=$1 OFS $2 OFS $3 OFS $4 # gather constants to one element
if(NR==1)
a[NR][4]="DOC_DATE" OFS a[NR][4] OFS "QTY"
}
END { # everything is in memory
print a[1][4]; # header print
for(j=5;j<=n;j++) # loop all data fields
for(i=2;i<=NR;i++) # loop all records
print a[1][j],a[i][4],a[i][j] # output
}' file
DOC_DATE BRANCH_CODE DEPT_CODE ITEM_CODE UNIT_CODE QTY
01/04/2017 KI-01 DP-0001 10001 KG 31.5
01/04/2017 KI-01 DP-0001 10002 KG 22
02/04/2017 KI-01 DP-0001 10001 KG 45
02/04/2017 KI-01 DP-0001 10002 KG 0
03/04/2017 KI-01 DP-0001 10001 KG 72