开发者

Syntax error in expression ( error token is "i=2" )

for (( int i="$4"; i<"$5"; i++ ))
do
     awk "NR==i{print}" $1
done

I want awk to print out the records between $4 and $5 ( itll be a range, ie 4-9 )

cant开发者_运维技巧 figure out why im getting this error?

Syntax error in expression ( error token is "i=2" )


This is not C. You don't need to declare the type of the counter.


awk doesn't know anything about the shell variable i, for a start, Why would you just not use:

awk "(NR >= $4) && (NR <= $5) {print}" $1

The shell itself should expand the $ variables since they're within double quotes rather than single quotes (this is shell-dependent of course but covers the most popular ones, primarly bash).

You can see this in action in the following transcript:

====
pax$ cat infile
..1
..2
..3
..4
..5
..6
..7
..8
..9

====
pax$ cat qq.sh
#!/usr/bin/bash

awk "(NR >= $4) && (NR <= $5) {print}" $1

====
pax$ ./qq.sh infile junk junk 2 5
..2
..3
..4
..5

====
pax$ _


Here proper syntax:

for ((  i = $2 ;  i <= $3;  i++  ))
do
   awk "NR==$i{print}" $1
done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜