Prev: Useful use of cat? (was Re: This Week's Useless Use of Cat Award goes to...)
Next: Retain quotes when passing arguments to another script
From: pk on 24 Feb 2010 08:59 Ed Morton wrote: > The OP had this concern: > >> It works fine now but what will happen if >> each file has different rows? > > so apparently you can't assume he'll have the same number of lines in > every file and particularly you can't assume the number of lines in the > last file read will be the max number of lines. Good catch, I had missed that. > He doesn't say what to do in that case, but we could do this: > > awk 'NR==FNR{out[FNR]=$1 OFS $2 OFS $3 OFS $4 OFS $5} > {out[FNR]=out[FNR] OFS $7 OFS $9; maxFnr=(FNR > maxFnr ? FNR : > {maxFnr)} > END {for(i=1;i<=maxFnr;i++) print out[i]}' file1 file2 file3 ... file100 > > Note the change from FNR to maxFnr in the END loop. > > There's probably more needs to be done so the "columns" don't get > left-shifted if there's fewer lines in some files but until the OP tells > us what he wants (e.g. populating some "NULL" value in columns if missing > lines) there's not much point guessing any further.... Agreed.
From: ezhil on 24 Feb 2010 10:20
On Feb 24, 1:59 pm, pk <p...(a)pk.invalid> wrote: > Ed Morton wrote: > > The OP had this concern: > > >> It works fine now but what will happen if > >> each file has different rows? > > > so apparently you can't assume he'll have the same number of lines in > > every file and particularly you can't assume the number of lines in the > > last file read will be the max number of lines. > > Good catch, I had missed that. > > > He doesn't say what to do in that case, but we could do this: > > > awk 'NR==FNR{out[FNR]=$1 OFS $2 OFS $3 OFS $4 OFS $5} > > {out[FNR]=out[FNR] OFS $7 OFS $9; maxFnr=(FNR > maxFnr ? FNR : > > {maxFnr)} > > END {for(i=1;i<=maxFnr;i++) print out[i]}' file1 file2 file3 ... file100 > > > Note the change from FNR to maxFnr in the END loop. > > > There's probably more needs to be done so the "columns" don't get > > left-shifted if there's fewer lines in some files but until the OP tells > > us what he wants (e.g. populating some "NULL" value in columns if missing > > lines) there's not much point guessing any further.... > > Agreed. Thank you very much PK and Ed for the help. It works fine. |