0

When I run my script, there is no output to the terminal.

This my code:

for ((h = 1 ; h < 4 ; h++ )); do 
    x=$(awk -v i=h -v j=17 'FNR == i {printf "%s ", $j}' newiptables.log)
        echo $x
done

This is the file

Dec 26 09:17:51 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=313 TOS=0x00 PREC=0x00 TTL=64 ID=59334 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK PSH URGP=0 
Dec 26 09:17:52 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=1440 TOS=0x00 PREC=0x00 TTL=64 ID=47303 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK URGP=0 
Dec 26 09:17:52 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=1440 TOS=0x00 PREC=0x00 TTL=64 ID=47559 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK URGP=0 
codeforester
  • 39,467
  • 16
  • 112
  • 140
Sanshayan
  • 1,076
  • 1
  • 11
  • 16
  • Did you mean to write `i=$h` when invoking your awk script? – xaizek Dec 26 '13 at 07:28
  • Are you sure? It outputs `SPT=80` three times for me. – xaizek Dec 26 '13 at 07:31
  • sorry i've made a mistake – Sanshayan Dec 26 '13 at 07:33
  • Closely related questions: [SO 20742474](http://stackoverflow.com/questions/20742474), [SO 20780315](http://stackoverflow.com/questions/20780315), [SO 20780890](http://stackoverflow.com/questions/20780890), [SO 20781258](http://stackoverflow.com/questions/20781258). Not quite duplicates, but the same data stream. – Jonathan Leffler Dec 27 '13 at 15:00

1 Answers1

1

You are saying:

awk -v i=h -v j=17 ...

Note i=h which is equivalent to saying: i=0.

It seems that you wanted to say:

x=$(awk -v i=$h -v j=17 'FNR == i {printf "%s ", $j}' newiptables.log)
             ^
             |== refer to the variable h
devnull
  • 118,548
  • 33
  • 236
  • 227