-1

I have a series of .txt files with tables of numbers, and I extract form each file the number I want. Example:

for f in $(find . -name "E_total_conv.txt"); do cat $f | tail -1 | awk '{print $2}' ; done

The output:

-1261.21295697266896
-1261.21748935324558
-1261.21885728710254
-1261.21737642551761
-1261.21337619301949
-1261.20719583615892

What I want is to add a first colum which will be used as the labels for the x axis when plotting with gnuplot. I would like to know how can sort of insert a list of strings there as a column, and better - how can dictate some sort of mathematical list to be inserted? I mean like "even numbers from 1 to 10", and so on.

Example for requested output:

0.0    -1261.21295697266896
0.2    -1261.21748935324558
0.4    -1261.21885728710254
0.6    -1261.21737642551761
0.8    -1261.21337619301949
1.0    -1261.20719583615892
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Argento
  • 3
  • 3
  • `for f in $(find . -name "E_total_conv.txt"); do` and `cat $f |` are both antipatterns. The right way to do whatever you're trying to do will not do either of those things. – Ed Morton Oct 11 '21 at 20:50

3 Answers3

1

Remove

| awk '{print $2}'

and append after done

| awk '{printf("%.1f %s\n", (NR-1)/5, $2)}'

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

Edit: take awk out of the loop, and it can maintain the count:

for ... do ... done | awk 'BEGIN { x = 0; } { printf("%.1f %s\n", x, $2); x += 0.2; }'

Output in my test:

0.0 -1261.21295697266896
0.2 -1261.21748935324558
0.4 -1261.21885728710254
0.6 -1261.21737642551761
0.8 -1261.21337619301949
1.0 -1261.20719583615892

You can increment x by whatever you want. For example to make them a series of even numbers, start with 0 and increment by 2.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
0

Untested since you didn't prove any sample input but this:

awk 'ENDFILE{print (ARGIND-1)/5, $2}' $(find . -name "E_total_conv.txt")

might be all you need (using GNU awk for ENDFILE and ARGIND) assuming your file names don't contain spaces and there aren't so many of them that they exceed the shell max args limit.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185