2

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

2
  • 2
    You only have one file to read , and that is file.txt correct?, so the for loop actually is reading file.txt correct? If so, then when "5," is detected, you would have already deleted those lines using sed. On subsequent detection of "5," , there will be no lines to delete. but you will still be callling sed. That's your overhead. show a clearer example of your script (don't show it halfway) and show any input files and expected output. Commented Apr 21, 2011 at 9:40
  • Note that [ is a command, so it requires a space after it: if [ "$Y" = "5," ]; then ...; fi. Shell syntax is not really C-like. Commented Apr 21, 2011 at 13:07

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

thanks, now I have no error but when I run my script nothing happened
I updated my answer. This code snippet should work if your file has three columns and you're looking for "5," in the third column. If it doesn't work, show us more of your code.
the file have more than 3 columns
OK, I edited the answer. It should have at least three columns. x x Y are the first three columns.
@Glist: using a while loop with read is idiomatic shell script. It is aconventional to read both the first two columns into the same variable; you would normally use read junk x y (using three different names, and keeping the case consistent - upper-case for environment variables, and lower-case for non-environment variables). Classically, double-equals was an error; it generally works these days.
|
0

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

1 Comment

How does your answer differ from that of Zsolt Botykai?
0

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

2 Comments

awk works good, it print 5 or fail but nothing change in the file, this : $0 !~ "^[0-9][0-9]*, [0-9][0-9]*, 6,") print $0 >> "file.txt" it's not executed, do you know why?
Maybe you are using the same output file as you input?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.