Regex escape special character in AWK if statement

I am having issues escaping special characters in my AWK script as follows:

for id in `cat file`
do
grep $id in file2 | awk '\
    BEGIN {var=""} \
    { if ( /stringwith+'|'+'50'chars/ ) {
        echo "do this"
    } else if ( /anotherString/ ) {
        echo "do that"
    } else {
        echo "do other"
    } }'
done

I am not sure how I am supposed to escape the multitude of characters in the regex string "stringwith+'|'+'50'chars"

Either use:

awk -vW="stringwith[+]'[|]'[+]'50'chars" 'BEGIN {var=""}
    {
      if ($0 ~ W) {
        print "do this"
    } else if ( /anotherString/ ) {
        print "do that"
    } else {
        print "do other"
    } }'

or

awk 'BEGIN {var=""}
    {
      if ( /stringwith[+]'\''[|]'\''[+]'\''50'\''chars/ ) {
        print "do this"
    } else if ( /anotherString/ ) {
        print "do that"
    } else {
        print "do other"
    } }'

How about you start by telling us what is in your input file and what are you trying to get from it. It makes all the difference.

This works with modern awk - POSIX character classes

awk  '/stringwith+[[:punct:]]|[[:punct:]]+[[:punct:]]50[[:punct:]]chars/' somefile

mirni is right - what are you trying to do, not how you decided to do it....

I have a file that is a parsed together from several other files. Each line has a unique identifier that establishes which file it came from:

data data UID data ...

So, for each of these UID's I want to grab some value (value1). Once I find that value there are 3 potential outcomes I can obtain depending on particular Strings I find which succeed the original value:

data data UID data=data=value1...
...
data data UID data data (xyzstring)...

So, when I find xyzstring as "stringwith+'|'+'50'chars" I want to append certain text to a csv file, similarly with "anotherString", giving me the following shell script:

grep 'string' file2 | awk '{print $3}' > file
echo "this,that" > file.csv
for id in `cat file`
do
grep $id in file2 | awk '\
    BEGIN {var=""} \
    /knownString/ {split($0, tmp, "="); var=tmp[3]} \
    { if ( /stringwith+'|'+'50'chars/ ) {
        echo var",This" >> file.csv
    } else if ( /anotherString/ ) {
        echo var",That" >> file.csv
    } else {
        echo var",Other" >> file.csv
    } }'
done

I am fairly new to scripting and would be open to any suggestions.