0

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?

AngelikaG
  • 11
  • 1
  • How do you want your matrix to look like and single quote the `Awk` command, else it will treat `$1` and `$2` are positional arguments which will expand to nothing in this case – Inian Nov 30 '17 at 15:21
  • What exactly should be the format of the output matrix? I'm not sure I fully understand what you're trying to achieve. – Nepho Nov 30 '17 at 15:34
  • I have raster of healthy and dead trees. This result shows numbers of pixels from one raster that cover the same class in other raster. I would like to have matrix 2x2 with: healthy-healthy, healthy-dead, dead-healthy, dead-dead. But firstly just to save it in proper way to text file. – AngelikaG Nov 30 '17 at 15:36
  • 1
    Please add your desired output for that sample input to your question. – Cyrus Nov 30 '17 at 15:46
  • For example, this data should be printed like that: first row: 151,473 second row: 12030, 197. as a matrix 2x2 – AngelikaG Nov 30 '17 at 16:03
  • what happens to the first and last row? What are the rules? Skip first row and combine the second fields unit the last line of the input file? – karakfa Nov 30 '17 at 16:25

2 Answers2

0

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
Yoda
  • 435
  • 2
  • 7
0

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
karakfa
  • 66,216
  • 7
  • 41
  • 56