Nikolaus
Nikolaus

Reputation: 253

Round numbers and divide them to 2 columns

Now it looks like:

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

It should look like:

0 15.39
15.39 27.59
27.59 46.06
46.06 65.68
     .
     . 
     .

Hi I have .txt file from which I filtered only this xmin, xmax numbers with command:

sed -n '16,$p' info.txt | grep "xmin\|xmax"

I know how to show only numbers but I don't know how to round this numbers and divide them into 2 columns. The result should look like the example.

Upvotes: 0

Views: 78

Answers (4)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 'N;s/xm.. = //g;s/\([0-9]*\.[0-9]\{2\}\)[0-9]*/\1/g;y/\n/ /' info.txt
  • read 2 lines
  • remove the xman/xmin =
  • trunc the number
  • change new line to space
  • cycle

(POSIX version so --posix for GNU sed)

Upvotes: 0

bernhard
bernhard

Reputation: 1

    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" };
    }

running this perl script with:

    script.pl < data 

would result in

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

Upvotes: 0

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

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

Print the last field ($NF) in each line with format specifier (%.2f). Print newline after every second line.

Upvotes: 1

Kent
Kent

Reputation: 195049

awk could do it easily:

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

with your example:

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

Upvotes: 3

Related Questions