How can I print the filename and the result of 1/2*(a+b), where a is the 1st field of the 2nd line and b is the 1st field of the last line please? I do not know how to save the values into variables as constants. Many thanks

awk -v a=$(NR==2{print $1}) b=$(END(print $1)) '{print FILENAME, 1/2*(a+b)}' rv*.out 


4 5 6
14545.5886 2 6
2 3 5
45457.5462 8 6


1 2 
34441.4545 8
6 8
8 8 
54447.4545 1

Output for:

for file in test1.txt test2.txt; do 
    awk ' (NR==2){a=$1} END{print FILENAME, 1/2*(a+$1)}' "$file" >> stredni.txt; 

test1.txt 7272.79
test2.txt 17220.7

Desired result:

test1 30001.5674
test2 44444.4545
  • Thanks. I made an edit that should help clarify, please let me know if that is not what you meant. And can you please explain what you want to store in variables? What kind of variables? Where would you use these variables? In the same awk script? In a different command? – terdon May 6 at 17:51
for i in  "p.txt" "l.txt"; do co=`wc -l $i| awk '{print $1}'`; awk -v co="$co" 'NR==2{a=$1}NR==co{b=$1}END{print FILENAME,1/2*(a+b)}' $i; done


p.txt 30001.6
l.txt 44444.5

The syntax var=$(something) is shell syntax (and not awk) which means "run the command something and store the result in the shell variable $var". You seem to be expecting it to be somehow linked to your awk script since NR==2 etc are awk statements. That won't work.

This command will print the output you expect:

$ for file in file1 file2; do 
    awk ' (NR==2){a=$1} END{print FILENAME, 1/2*(a+$1)}' "$file"; 
file1 30001.6
file2 44444.5

You can do it using GNU awk (gawk) also:

$ gawk 'FNR==2{a=$1}ENDFILE{print FILENAME, 1/2*(a+$1)}' file1 file2
file1 30001.6
file2 44444.5
  • How is it with decimal places? I have four decimal places in 1st column, but the result give only one or zero. – Elena Greg May 6 at 18:20
  • 1
    @ElenaGreg first, please make sure that the examples you give accurately represent your data. That said, this should work fine with decimals, that isn't a problem. If this doesn't work, then please edit your question and add an actual example file and the command you ran and the output you got. Are the decimals maybe 0? – terdon May 6 at 18:24
  • I edited the question. Thank you – Elena Greg May 6 at 18:35
  • @ElenaGreg I just ran the exact commands from your question (and my answer) and got the expected result (see updated answer). If you run this on the exact example files you give in your question do you not get the result I show? – terdon May 6 at 18:42
  • 3
    The value of $1 (or $0 or any other field) in the END section is undefined by POSIX. In some awks (e.g. gawk) it'll be the value it had when the last record was read, in other awks (apparently whichever one the OP is using) it'll be null, and in others still it could be anything else. For portability to all awks you need awk 'NR==2{a=$1} {b=$1} END{print FILENAME, 1/2*(a+b)}' "$file" – Ed Morton May 6 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.