Вы можете использовать утилиты tbl
, nroff
, если не хотите возиться с шириной полей:
perl -F'\"?,\"?' -lane '
BEGIN {
chomp(@A = qx(sort -t/ -k1,1n -k2,2n < $ARGV[0]));
shift;
# prepare table for the tbl utiity
print q/.TS/;
print join(",", qw/allbox center/, qq/tab(\t);/) ;
print join($", qw/c s c/, qw/s/ x $#A) ;
print join($", qw/c r/, qw/c/ x @A), qw/./ ;
print join("\t", $,, q/interface switch/);
print join("\t", qw/VLAN-ID VLAN-NAME/, @A);
}
print
join("\t", @F[0,1], map { my $e = $_; (0 < grep { $e eq $_ } @F[2..$#F]) ? q[*] : q[] } @A);
END { print q/.TE/; }
' file-1 file-2 | tbl - | nroff -Tascii -ms | sed -n '/./!d;$!N;s/.*\n.\(.*\)./\1/p'
| interface switch
VLAN-ID | VLAN-NAME | 1/1 | 1/2 | 1/3 | 1/4 | 1/5 | 1/6 | 1/7 | 1/8 | 1/9 | 1/10 | 3/3
1 | vlan-wifi | * | | | | | | * | * | | |
2 | vlan-admin | | | * | | * | * | | | | |
3 | | | | * | | | | | | | | *
Использование GNU datamash
,split
(GNU coreutils )иawk
:
#!/bin/bash
# remove header line and split `input_file` into n files `split00000`, `split00001`...
# with max. 4 lines each (use `-l50` for your data file)
split -d -a5 -l4 <(tail -n+2 input_file) split
{ head -n1 input_file # add header
for fsplit in split*; do
mean=$(datamash -W mean 7 < "$fsplit") # calculate mean value
awk -v mean="$mean" '{ print $1,$2,$3,$4,$5,$6,mean,$8,$9 }' "$fsplit"
done
} | column -t > output_file # format as table and write result
rm split* # cleanup
В этом скрипте я использовал ваши данные (удалены пунктирные линии )в качестве входных данных и только 4 значения для среднего значения.
Замените -l4
на -l50
для вашего файла данных в сценарии. Это почти то же самое, что и вы, я просто позволяю split
и datamash
делать всю работу.
Входной файл:
$ cat input_file
trait effect snp chr pos snp_effect weight variance_explained var_a_hat
1 2 1 1 54 0.2030156E-02 1.251482 0 0
1 2 2 1 689 -0.3726744E-03 0.9660012 0 0
1 2 3 1 1234 0.4801369E-03 0.9823542 0 0
1 2 4 1 1280 -0.1104844E-03 0.9272357 0 0
1 2 5 1 2610 -0.1296295E-02 1.115933 0 0
1 2 481971 26 4897157 -0.7846317E-04 0.9226092 0 0
1 2 481972 26 4898314 -0.3934468E-03 0.9691408 0 0
1 2 481973 26 4898376 -0.7204678E-03 1.019935 0 0
1 2 481974 26 4898606 -0.1522481E-03 0.9333048 0 0
Результат:
$ cat output_file
trait effect snp chr pos snp_effect weight variance_explained var_a_hat
1 2 1 1 54 0.2030156E-02 1.031768275 0 0
1 2 2 1 689 -0.3726744E-03 1.031768275 0 0
1 2 3 1 1234 0.4801369E-03 1.031768275 0 0
1 2 4 1 1280 -0.1104844E-03 1.031768275 0 0
1 2 5 1 2610 -0.1296295E-02 1.0069045 0 0
1 2 481971 26 4897157 -0.7846317E-04 1.0069045 0 0
1 2 481972 26 4898314 -0.3934468E-03 1.0069045 0 0
1 2 481973 26 4898376 -0.7204678E-03 1.0069045 0 0
1 2 481974 26 4898606 -0.1522481E-03 0.9333048 0 0