|First, to use command substitution in the bash shell, do not use a $ sign during the assignment:|
a=`tail -20 /var/log/messages`
Second, I am not sure what you are trying to do as your requirements are vague, but it looks like you are going to need a 2-step process. I would send the output to a temp file instead of using a shell variable:
tail -20 /var/log/messages > tempfile.txt
Then, you can maybe use a combination of sed and grep to do what you want.
I like to use gres - global regular expression subsitution for string replacement purposes. It's discussed here:
You obviously have another requirement, but you are going to have to provide more information.