Sunday 15 February 2015

python - How to concatenate identifier specified on two rows? -



python - How to concatenate identifier specified on two rows? -

input identifier specified 2 rows 1-2

l1_i l1_i c-14 <---| unique idenfier wwpth wwpt wwpth <---| on 2 rows 1 2 3

goal: how concatenate rows?

l1_iwwpth l1_iwwpt c-14wwpth <--- unique identifier 1 2 3

p.s. take simplest , elegant solution.

assuming input in file called file:

$ awk 'nr==1{for (i=1;i<=nf;i++) a[i]=$i;next} nr==2{for (i=1;i<=nf;i++) printf "%-20s",a[i] $i;print"";next} 1' file l1_iwwpth l1_iwwpt c-14wwpth 1 2 3 how works

nr==1{for (i=1;i<=nf;i++) a[i]=$i;next}

for first line, save column headings in array a. then, skip on rest of commands , jump next line.

nr==2{for (i=1;i<=nf;i++) printf "%-20s",a[i] $i;print"";next}

for sec line, print column headings, merging ones first , sec rows. then, skip on rest of commands , jump next line.

1

1 awk's cryptic shorthand print line is. done lines after seconds.

tab-separated columns possible missing columns

if columns tab-separated:

awk -f'\t' 'nr==1{for (i=1;i<=nf;i++) a[i]=$i;next} nr==2{for (i=1;i<=nf;i++) printf "%s\t",a[i] $i;print"";next} 1' file

python unix sed concatenation

No comments:

Post a Comment