1
if [[ ! -z grep echo "${prof}" | cut -d. -f1 dm_smear.dat ]]

This gives me the following error. I am trying to find a string ${prof} in a file dm_smear.dat and if that exists in the file I will do certain operations

: syntax error in conditional expression   
: syntax error near `1`'   
: `         if [[ ! -z grep `echo "${prof}" | cut -d . -f 1` dm_smear.dat ]]'
2
  • What does the variable ${prof} contain? Given the amount if syntax errors and misconceptions in this short piece of code, you'd probably better explain exactly what should happen instead of have us guess. Commented Dec 17, 2013 at 5:23
  • An if statement is not a loop. Commented Dec 17, 2013 at 15:34

2 Answers 2

1

I am trying to find a string ${prof} in a file dm_smear.dat and if that exists in the file I will do certain operations

You can simply use the -q option for grep. Say:

if grep -q "${prof}" dm_smear.dat; then
   echo "Found the string"
   # Do something here
fi

You could fix your original code by using process substitution correctly:

if [[ ! -z $(grep $(echo "${prof}" | cut -d . -f 1) dm_smear.dat) ]];
   echo "Found the string"
   # Do something here
fi
Sign up to request clarification or add additional context in comments.

8 Comments

That just makes it quiet, still the loop doesn't run. I also tried if [[ ! -z cat dm_smear.dat | grep echo "${prof}" | cut -d. -f1 ]] but same error followed.
@Devansh Why are you executing: if [[ ! -z cat dm_smear.dat | grep echo "${prof}" | cut -d. -f1 ]]; ? Did you even try what was suggested in the answer?
Yes, I did! The loop doesn't run. I did it with and without -q with -q : loop doesn't run without -q gives the same error
@Devansh If grep doesn't find the string ${prof} in the file dm_smear.dat then the loop won't run. Isn't that what you wanted? Maybe you should update the question to clarify further if that isn't the case.
Yes, this is what exactly I want, but I've checked and string ${prof} exits in the file dm_smear.dat.
|
0

You can try following code

prof="KEY1"

if ! [ -z cut -d. -f1 dm_smear.dat | grep ${prof} ] ; then

     echo "FOUND"

else

     echo "NOT FOUND"

fi

In above code, we are searching KEY1 in dm_smear.dat file at first column

the output will be FOUND if file contains KEY1 in first column of any line, otherwise it prints NOT FOUND

where dm_smear.dat contains

KEY1.VALUE1

KEY2.VALUE2

KEY3.VALUE2

Comments

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.