开发者

Loop in shell script

Hi

I did a script in shell which read a file with some data inside:

for (( read x x Y ))
do
if ["$Y" == "5,开发者_开发技巧"]; then
    echo "5"
    sed -i '/^[0-9][0-9]*, [0-9][0-9]*, 6,/d' file.txt  
else 
    echo 'fail'
    fi
done

when I did it with a while loop it works but too long because the file is very big and do it line by line

so I would like to do it with a loop for and I get this error: syntax error near unexpected token 'done'

Can you help me?

Thanks

EDIT : I would like to know if it's possible to do something like this:

awk 'BEGIN {FS=", "}

{ if ( $3 == "5" )'

echo "5"

sed -i '/^[0-9][0-9]*, [0-9][0-9]*, 6,/d' newfile

'else { print "fail" } }' file


There are several syntax errors in your code.

Try this:

while read x x Y
do
  if [ "$Y" == "5," ]
  then
    echo "5"
    sed -i '/^[0-9][0-9]*, [0-9][0-9]*, 6,/d' file.txt  
  else
    echo 'fail'
  fi
done < YOUR_FILE

YOUR_FILE should have at least three columns (x x Y).


If you start an if in a shell script then you should also end it with fi


You need to close the if statement with fi before done.

You can do it in AWK too, without the loop:

awk 'BEGIN {FS="<SET YOUR FIELD SEPARATOR HERE>"}
     { if ( $3 == "5" ) { print "5" ; if ( $0 !~ "^[0-9][0-9]*, [0-9][0-9]*, 6,") print $0 >> "tmpfile" } else { print "fail" ; print $0 >> "tmpfile" } }' YOUR_INPUT_FILE

Then see the contents of tmpfile...

HTH

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜