[Beginner] AWK printing only specific rows

Hello, I got a very simple question. I'm new to AWK and I do not understand yet how a script works. I have a data file which contains numbers separated by spaces, what I want to achieve is to print the first row and the last one, then I use the values of a column to substract and get the result, however when doing it the script is printing on terminal all the rows of the input file. This is what I got

awk  "\x27" {
        if ( NR== 1 ){
          var=$2;
           print "First: ",var;
        }
}
END{
  if(NR==FNR){
    last =$2;
    print "Last: " ,last, "L-F: ", last-var;
  }
}
"\x27"

I am working on Mac and I have to replace every single quote ( ' ) with it's ASCII value, that' why my code has "\x27". Typing awk '{...}' will always get me an invalid character error each time I try to run. Anyway my output looks like this:

First: 131912079
131271999 131912079 -640080 0.216
(lots of rows, it prints the input file...)
142055743 142697039 -641296 0.064
Last: 142697039 L-F: 10784960

My expected output would be:

First: 131912079
Last: 142697039 L-F: 10784960

Please let me know what I am doing wrong, I am printing to a file so that I get what I want but still the input file is printed out on terminal and I don't want that. Greetings!

(...)
           print "First: ",var > "c.dat"
(...)
           print "Last: " ,last, "L-F: ", last-var >> "c.dat"

Welcome!
Why you cannot use a ' character? Are you really using a simple ASCII text editor?

Then, I do not see why it prints the input. There must be a true condition somewhere.? A further line with a lone non-null number somewhere in the main loop?

NR and FNR are line numbers, NR is total, and FNR is per file. For the first input file NR==FNR is true. But I do not understand why you test it, and that even in the END section.

The END section runs after reading the last input file. It is not 100% clear that the last input line is still present. Even if it seems to work for you I would use $2 in the main loop only.

awk '
  {
        if ( NR== 1 ){
          var=$2
          print "First: ",var
        }
        last=$2
  }
  END{
    print "Last: " ,last, "L-F: ", last-var
  }
'

I used TextEdit with plain text and ' character gets error. I created files with Atom and still get error, I also created files with nano and it does not work. My keyboard has Spanish ISO as input source and it just does not work. I also tried Unicode Hex Input with no luck.

Anyway, that's all the code I got, there are no more lines, no true conditions, not even a lone non-null number. I also don't know if testing NR==FNR is right, what I was thinking is a way to check that I am in the last row so that I can print the last row field. I am new to AWK and it's a headache to write programs and I am sad to say that copying your code and running it still prints the input file. :frowning_face:

Screen Shot 2020-10-04 at 13.45.16

Looks like each line is printed twice.
And a line

\x27

is a non-null number that is interpreted as a true condition and prints the input line.
So you have two such lines causing two times a print.

Do you see my code alright in a browser?
Can you mark/copy/paste it to your text editor?

I see the code perfectly fine in browser, I copied it all and pasted into terminal, press enter and the result is what I expect. However when I create a new file in Atom, paste the same text, go to terminal run file and get that annoying error. I reinstalled AWK via homebrew, I updated my bash to version 5.0.18(1), I changed my keyboard to english layout, nothing works.

Of course you can create a file in the terminal.
Run

cat > myscript.sh

Then paste the script.
Ensure the cursor is at the beginning of a new line, or hit Enter key to force a new line.
Then terminate the input with a Ctrl-D

I am sorry it does not work either.Screen Shot 2020-10-04 at 16.24.57

Oh no, it is not a plain awk script!
It is a shell script that runs an awk command. The code between the ' ' is the (embedded) awk code.

A shell script you run with

sh myscript.sh

done! Thank you so much :grinning: So now I think I am creating all my files in a shell script. Greetings!