I have a file (Data.txt) with something like this:A=1234597890 B=192.168.1.1sometimes B goes first and sometimes only A or B.So, If I find:  A,    Print A content and execute A script  B,         B                  B    A&B,       A&B                 A   Neither, Print a error messageNow the problem is the content in A or B is not the same always!I don't know which would be the best command to use.Thank you for your help and for trying to understand my basic English :)!
Find string in text file and execute a script depending of the result
text processing;scripting;string
You can do this in Perl:$ perl -lne 'if(/A=([^\s]+)/){print A : $1; `scriptA.sh`}            if(/B=([^\s]+)/){print B : $1; `scriptB.sh`}' Data.txtExplanation-lne : remove trailing newlines from each input line and add a newline to each print call (-l); read the file line by line (-n) and run the script given by -e on each line. if(/A=([^\s]+)/){print A : $1; `scriptA.sh`} : if this line matches A= something, print A : something (the parentheses mean that the pattern is captured and is now saved as $1), then run scriptA.if(/B=([^\s]+)/){print B : $1; `scriptB.sh`} : as above but for B. Or, you can just extract the values and parse them:$ grep -Po '[AB]=[^\s]+' Data.txt |     while IFS== read name val; do         echo $name=$val;         [ $name = A ] && scriptA.sh || scriptB.sh;     doneExplanationgrep -Po '[AB]=[^\s]+' Data.txt : The -P enables PCREs, and the -o will cause grep to print each match on a different line. The [AB]=[^\s]+ means match A or B, then =, then as many non-whitespace characters as possible. The output of this is the full collection of A=foo and B=bar from your input file, each on its own line. while IFS== read name val : set the ionput field separator to = and read two variables. This means that $name will be A or B and $value will be the value associated with them.echo $name=$val; : print them, change the format to whatever you like.[ $name = A ] && scriptA.sh || scriptB.sh; : if $name is A, run scriptA.sh, else run scriptB.sh. Both of the solutions above assume that the scripts you want to call are scriptA.sh and scriptB.sh and that they are in your $PATH. Edit accordingly.