-1

I want to read a file back ward while using a variable present in the first line (here: 2636).

My file:

nx_ 2355 ny_ 2636
0.000000 0.000000 0.000000
1.000000 68.000000 0.428139
2.000000 68.000000 0.939878
3.000000 67.000000 0.757181
4.000000 68.000000 0.000000
5.000000 69.000000 -1.229728

To read the file forward, and process it, I used:

cat $1 | awk 'NR==1 {nb=$4} NR>1 {up=nb-$1; print $2,up,$3}'

To read the file backward it seems I should use tac, but I don't know how to retrieve the variable in the first line, and avoid to process the last line. I am searching for something like this:

tac $1 | awk 'NR==END {nb=$4} NR<END {up=nb-$1; print $2,up,$3}'

I want to have as output:

69.000000 2631 -1.229728
68.000000 2632 0.000000
67.000000 2633 0.757181
68.000000 2634 0.939878
68.000000 2635 0.428139
0.000000 2636 0.000000
Argent
  • 33
  • 6
  • 1
    [edit] your question to state **what** you're trying to do rather than just **how** (read X then read the rest backwards) you're trying to do it and add the expected output given your posted sample input so we can help you. – Ed Morton Jul 01 '19 at 14:50
  • cat $1 | rev| and so on... can you give information on what you are trying to achieve as well – yoga Jul 01 '19 at 15:08
  • @yoga that'd be a UUOC plus `rev` is for reversing characters in a line, not lines in a file. – Ed Morton Jul 01 '19 at 15:10
  • ok. i understood the question wrongly then. i need more inforation on what he really needs. – yoga Jul 01 '19 at 15:11
  • 1
    I edited my question. And I am a she :) (I would use 'they' if I don't know the gender). – Argent Jul 02 '19 at 08:55

2 Answers2

0

Is this what you're trying to do?

$ awk 'NR==1{nb=$4; next} {print $2, nb-$1, $3}' file | tac
69.000000 2631 -1.229728
68.000000 2632 0.000000
67.000000 2633 0.757181
68.000000 2634 0.939878
68.000000 2635 0.428139
0.000000 2636 0.000000

If you really did have to do what you said you wanted to do then that'd be this:

$ read -r _ _ _ nb < file; tail +2 file | tac | awk -v nb="$nb" '{print $2, nb-$1, $3}'
69.000000 2631 -1.229728
68.000000 2632 0.000000
67.000000 2633 0.757181
68.000000 2634 0.939878
68.000000 2635 0.428139
0.000000 2636 0.000000

or this:

$ read -r _ _ _ nb < file; tac file | awk -v nb="$nb" 'NR>1{print p[2], nb-p[1], p[3]} {split($0,p)}'
69.000000 2631 -1.229728
68.000000 2632 0.000000
67.000000 2633 0.757181
68.000000 2634 0.939878
68.000000 2635 0.428139
0.000000 2636 0.000000

or similar but it seems unlikely that's what you really need given the script you posted.

If the above doesn't answer your question then edit your question to clarify your requirements and provide the expected output given your posted sample input.

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

You could split and rejoin the two parts of the file?

Something like:

cat <(head "$1") <(tail +2 "$1"| tac)  

But probably you are better doing the head "$1" command to a variable, then doing the tail +2 "$1"| tacseparately into a read loop.

Gem Taylor
  • 5,381
  • 1
  • 9
  • 27