save output stats to file using bash

66 Views Asked by At

I am working with GRASS GIS using PuTTy console and I would like to save statistics that I received, to text file.

> r.stats -c xyz
1 286048
2 151
3 473
4 12030
5 197
* 107401

I want to use awk to create matrix, but my problem is to save result of proper command that I used.

I know that in general it could be like:

> awk -F "{print $1 $2}" from >> to

But how should it look like in my case?

2

There are 2 best solutions below

0
On

You can try something like:-

awk '!(NR%2){printf "%d\n", $2}NR%2{printf "%d\t", $2 }' file
286048  151
473     12030
197     107401
0
On

I think you have other rules that are not specified in the question based on the expected output in the comments. This will print based on my interpretation of your requirements: skip first line, pair the following lines' second fields and only print pairs...

$ awk 'NR==1{next} {if(NR%2) print v, $2; else v=$2}' file

151 473
12030 197