awk -v OFS="\t" '
FNR==1{ # Read first line
max=NF # save no of fields
print # print header
next # go to next line
}
NF>max{ # if no of fields are greater than max fields
split($0,fd) # split record by fields sep, and store in array fd
NF=max-1 # set NF to max-1
for(i=max; i in fd; i++) # loop through fd from max
print $0,fd[i]; # print modified record, and field
next # stop processing go to next
}$1=$1 # $1=$1 is for making tab as o/p field separator for unmodified record
' file
輸入
$ cat f
id country place
1 A mall park
2 B beach
3 C hotel resort
4 D museum
5 E garden
輸出
$ awk -v OFS="\t" 'FNR==1{max=NF; print; next}NF>max{split($0,fd); NF=max-1; for(i=max; i in fd; i++)print $0,fd[i]; next}$1=$1' f
id country place
1 A mall
1 A park
2 B beach
3 C hotel
3 C resort
4 D museum
5 E garden
可能的重複[創建P在UNIX中的ivot表](http://stackoverflow.com/questions/39747893/creating-pivot-table-in-unix) – philshem