简体   繁体   English

用awk打印漂亮?

[英]Pretty-print with awk?

I have a code which is intended to output numbers stored in a file (which are in one column) to another TXT file. 我有一个代码,用于将存储在文件(在一列中)中的数字输出到另一个TXT文件。 The part of the code which does this this is: 执行此操作的代码部分是:

awk -F"\n" 'NR==1{a=$1"    ";next}{a=a$1"    "}END{print a}' col_trim.txt >> row.txt

the output is something like this: 输出是这样的:

1.31    2.3    3.35    2.59    1.63
2.03    2.21    1.99    1.5    1.12 
1    0.6    -0.71    -2.1    0.01 

But I want it to be like this: 但我希望它是这样的:

1.31    2.30    3.35    2.59    1.63
2.03    2.21    1.99    1.50    1.12 
1.00    0.60   -0.71   -2.10    0.01 

As you see all numbers in the second sample have 2 digits after decimal and also if they are negative, the negative sign is placed before the number so it doesn't mess the arrangement of the numbers. 如您所见,第二个样本中的所有数字在小数点后还有两位数字,并且如果它们都为负数,则在数字之前放置负号,这样就不会弄乱数字的排列方式。

Any idea? 任何想法?

PS: The input file is a text file with a column of numbers (for each row): PS:输入文件是一个文本文件,带有一列数字(每行):

1.31
2.3
3.35
2.59
1.63

The whole code is like this: 整个代码是这样的:

#!/bin/sh

rm *.txt
for time in 00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96; do
    filename=gfs.t00z.master.grbf$time.10m.uv.grib2
    wgrib2 $filename -spread $time.txt
    sed 's:lon,lat,[A-Z]* 10 m above ground d=\([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\).*:\1 '$time'0000:' $time.txt > temp.txt
    for (( j = 1; j <= 2; j++ )); do
        if [ j == 1 ]; then
            sed -n '/lon,lat,UGRD/,/lon,lat,VGRD/p' $time.txt > vel_sep.txt
        else
            sed -n '/lon,lat,VGRD/,/359.500000,90.000000/p' $time.txt > vel_sep.txt
        fi
        line=174305
        sed -n 1p temp.txt >> row.txt
        for (( i = 1; i <= 48;  i++ )); do
            sed -n "$line","$(($line+93))"p vel_sep.txt > col.txt
            sed 's:[0-9]*.[0-9]*,[0-9]*.[0-9]*,::' col.txt > col_trim.txt   
            awk -F"\n" 'NR==1{a=$1"    ";next}{a=a$1"    "}END{print a}' col_trim.txt >> row.txt
            line=$(($line-720))
        done
    done
done

exit 0

You can use the column command: 您可以使用column命令:

awk -F"\n" 'NR==1{a=$1"    ";next}{a=a$1"    "}END{print a}' col_trim.txt | \
    column -t >> row.txt

This gives: 这给出:

1.31  2.3   3.35   2.59  1.63
2.03  2.21  1.99   1.5   1.12
1     0.6   -0.71  -2.1  0.01

Replace your awk by this: 用以下内容替换您的awk:

awk -F"\n" 'NR==1{a=sprintf("%10.2f", $1); next}
           {a=sprintf("%s%10.2f", a,$1);}END{print a}' col_trim.txt >> row.txt

EDIT: For left alignment: 编辑:对于左对齐:

awk -F"\n" 'NR==1{a=sprintf("%-8.2f", $1); next}
           {a=sprintf("%s%-8.2f", a,$1);}END{print a}' col_trim.txt >> row.txt

This can be solved using printf with awk 可以使用带有awk printf来解决

Eksample: 样品:

echo -e "1 -2.5 10\n-3.4 2   12" | awk '{printf "%8.2f %8.2f %8.2f\n",$1,$2,$3}'
    1.00    -2.50    10.00
   -3.40     2.00    12.00

Additionally, this script has big spaces we can improve. 此外,此脚本还有很大的空间可以改进。

Here is the first one: 这是第一个:

change from: 从:

for time in 00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96; do

to

for time in $(seq 0 3 96); do
    time=$(printf "%02d" $time)

if you can show us the sample output of wgrib2 $filename -spread $time.txt , we can give more suggestions. 如果您可以向我们展示wgrib2 $filename -spread $time.txt的示例输出,我们可以给出更多建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM