Glist
Glist

Reputation: 79

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

Upvotes: 2

Views: 665

Answers (3)

Zsolt Botykai
Zsolt Botykai

Reputation: 51593

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

Upvotes: 0

lecodesportif
lecodesportif

Reputation: 11049

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).

Upvotes: 2

Vijay
Vijay

Reputation: 67211

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

Upvotes: 0

Related Questions