舍入数字并将它们分成2列

时间:2015-03-04 10:32:58

标签: sed grep numbers rounding

现在看起来像:

xmin = 0
xmax = 15.393970521541949
xmin = 15.393970521541949
xmax = 27.58997052154195
xmin = 27.58997052154195
xmax = 46.05797052154195
xmin = 46.05797052154195
xmax = 65.67797052154194
xmin = 65.67797052154194
xmax = 81.08048969617173
xmin = 81.08048969617173
xmax = 82.7959410430839

应该看起来像:

0 15.39
15.39 27.59
27.59 46.06
46.06 65.68
     .
     . 
     .

嗨我有.txt文件,我只用命令:

过滤了这个xmin,xmax数字
sed -n '16,$p' info.txt | grep "xmin\|xmax"

我知道如何仅显示数字,但我不知道如何舍入这些数字并将它们分成2列。结果看起来应该是示例。

4 个答案:

答案 0 :(得分:3)

awk可以轻松完成:

awk '{printf "%.2f%s",$3,!(NR%2)?"\n":FS}' file

以你的例子:

kent$  cat f
xmin = 0
xmax = 15.393970521541949
xmin = 15.393970521541949
xmax = 27.58997052154195
xmin = 27.58997052154195
xmax = 46.05797052154195
xmin = 46.05797052154195
xmax = 65.67797052154194
xmin = 65.67797052154194
xmax = 81.08048969617173
xmin = 81.08048969617173
xmax = 82.7959410430839

kent$  awk '{printf "%.2f%s",$3,!(NR%2)?"\n":FS}' f
0.00 15.39
15.39 27.59
27.59 46.06
46.06 65.68
65.68 81.08
81.08 82.80

答案 1 :(得分:1)

awk '{printf("%.2f ", $NF)} !(NR%2){printf("\n")}' File

使用格式说明符($NF)打印每行中的最后一个字段(%.2f)。在每newline行之后打印second

答案 2 :(得分:0)

    my $mi,$ma;
    while (<>) {
        if (/xmin = ([0-9.]*)/) { $mi=sprintf("%.2f", $1); next; };
        if (/xmax = ([0-9.]*)/) { $ma=sprintf("%.2f", $1); printf "$mi $ma\n" };
    }

使用以下命令运行此perl脚本:

    script.pl < data 

会导致

0.00 15.39
15.39 27.59
27.59 46.06
46.06 65.68
65.68 81.08
81.08 82.80

答案 3 :(得分:0)

sed 'N;s/xm.. = //g;s/\([0-9]*\.[0-9]\{2\}\)[0-9]*/\1/g;y/\n/ /' info.txt
  • 阅读2行
  • 删除xman/xmin =
  • 截断数字
  • 将新行更改为空格
  • 周期

(对于GNU sed,POSIX版本为--posix