3

I want to test if a pattern exists in a file.

  1. If the pattern exists (In a file among a list), do a replacement and update the corresponded file.
  2. If the pattern doesn't exists, 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.

Input of file 1

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

Input of file2

Rho =  1.1955904623E+02
Another.Chemist
  • 2,386
  • 3
  • 29
  • 43

1 Answers1

2

You could simply go ahead with the substitution, if the pattern exist something will be changed, if not, nothing will be done to the file.

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

check this question.

Community
  • 1
  • 1
Tarc
  • 3,214
  • 3
  • 29
  • 41