linux - How to split data into two new record in Unix? -
i have data need separate record 2 new record. sample data this:
id country place 1 mall park 2 b beach 3 c hotel resort 4 d museum 5 e garden
i want data become this:
id country place 1 mall 1 park 2 b beach 3 c hotel 3 c resort 4 d museum 5 e garden
the data tab delimited. tried using sed
, awk
, can't correct syntax. there other command can use can have desired output?
awk -v ofs="\t" ' fnr==1{ # read first line max=nf # save no of fields print # print header next # go next line } nf>max{ # if no of fields greater max fields split($0,fd) # split record fields sep, , store in array fd nf=max-1 # set nf max-1 for(i=max; in fd; i++) # loop through fd max print $0,fd[i]; # print modified record, , field next # stop processing go next }$1=$1 # $1=$1 making tab o/p field separator unmodified record ' file
input
$ cat f id country place 1 mall park 2 b beach 3 c hotel resort 4 d museum 5 e garden
output
$ awk -v ofs="\t" 'fnr==1{max=nf; print; next}nf>max{split($0,fd); nf=max-1; for(i=max; in fd; i++)print $0,fd[i]; next}$1=$1' f id country place 1 mall 1 park 2 b beach 3 c hotel 3 c resort 4 d museum 5 e garden
Comments
Post a Comment