i<=: Expression is not complete; more tokens expected. ksh

北战南征 提交于 2019-12-25 04:13:14

问题


i get this error: i<=: Expression is not complete; more tokens expected. This is the code:

vr=`$line`
set sep_mx=`echo $vr | awk '{
    n=split($0,x,"@#;")
    print n
}'`

echo $sep_mx
i=1 && while ((i<=$sep_mx))
do                                       
    echo $vr | awk -v er=$i '{
         n=split($0,x,"@#;")
         print x[er]
    }'
    ((i+=1))
done

Anyone can help me? Thanks


回答1:


To answer the original question, the error message already explains where the error occurs:

In i<= in i<=: Expression is not complete; more tokens expected. is the result of the expansion of the contents of ((i<=$sep_mx)).

This would happen if $sep_mx contained an empty string, which in turn, would be the consequence of

vr=`$line`

resulting in a vr that does not contain the delimiter string you include in the split() function in the awk line.

Note that awk split()'s third argument is a regular expression. If you want to split on any of the characters in "@#;", you should turn it into a character set by enclosing it in square brackets.

Compare:

$ echo "@fee#fie@foe" | awk '{ print split($0,x,"[@#;]") }'
4

With:

$ echo "@fee#fie@foe" | awk '{ print split($0,x,"@#;") }'
1



回答2:


ok , got that thing working.

Logic : code that would take a string , break it into words based on a defined delimiter and put each as a array element.

To make things a bit more simple i removed the other two delim option (# and ;) specified in your question.

working code :

Kaizen ~/so_test $ cat zawk1.sh
echo "entter a line : " ;
read line ;

vr="$line";

      sep_mx=`echo $vr| awk '{ n=split($0,x,"@"); print n }'` ; 
      echo $sep_mx ;  ## here the number of substring produced is calc.

      while [ $i -le $sep_mx ]
      do
          ## would print individual substring one at a time as the counter increases.
          echo $vr | awk -v er=$i '{ n=split($0,x,"@"); print x[er] }' ;
          i=$(( $i + 1 ));
      done

output :

 Kaizen ~/so_test $ ./zawk1.sh
 entter a line :
 hello@world@how@are@you
 5
 hello
 world
 how
 are
 you

hope this helps !!



来源:https://stackoverflow.com/questions/16942832/i-expression-is-not-complete-more-tokens-expected-ksh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!