Check if the pattern exists in the file, evaluate if

I want to check if a template exists in a file.

  • If the template exists (in the file among the list), replace and update the corresponding file.
  • If the template doesn't exist, do nothing.

So far I have done:

    if [ -f $((`awk '/Pattern1/' $FILE`))];then
        sed 's/Pattern1/\nWORD/g' $FILE > a
        mv a $FILE
    fi

      

But, when I test it, I get the following error:

syntax error: invalid arithmetic operator

      

And I don't want to evaluate the numeric part, just if the pttern ~ string is there or not.

File input 1

Rho =  1.1955904623E+02     Rho at Nucleus =  1.1955904594E+02

      

File input2

Rho =  1.1955904623E+02

      

+3


source to share


1 answer


You could just continue replacing, if the template exists, something will be changed, if not, nothing will be done in the file.

awk '{sub(/Pattern1/, "\nWORD"); print}' $FILE > tmp && mv tmp $FILE

      



check this question .

+2


source







All Articles