Using variables in a regular expressions

I have the code, for example:

XYZ=5
ABC=`grep -e '^$XYZ,' x-ref | cut -f 2,3 -d , | sed -e 's/,/   #/'
echo $ABC

I have gotten creative in using escapes `\' and also brackets `{}', and combinations thereof. I want to use the variable in the grep statement.

The line in x-ref I want to retrieve is something like:

5,Carroll,064

Giving a result of "Carroll #064"

If I go out to a command-line and type:

grep -e '^$XYZ,' x-ref | cut -f 2,3 -d , | sed -e 's/,/   #/

It works just fine and brings me the result I want, but that is the problem, I need it in a script.

I was wondering how to escape out the '^$XYZ,' in grep, I have also used various -E, -F, egrep, and stuff. I was also curious, although I have found no concrete literature about this, if regexp or grep is treating the value of the variable as a field value.

The ultimate goal is having it in a larger script where the variables value will be a new incremented number in every pass.

I have used google.com to check this forum and others and have yet to find an answer. I am using SCO UNIX OpenServer 5.0.6, also this will be running on umm..., 5.0.5 and 5.0.2? versions so if anyone knows of issues with those they would be greatly appreciated also. I am not opposed to other utils for the purpose but using perl is not possible right now.

:frowning:

This seemed to work for me in ksh

more x-ref.test
XYZ=5
ABC=`grep -e "^$XYZ," x-ref | cut -f 2,3 -d , | sed -e 's/,/ #/'`
echo $ABC

more x-ref
5,Carroll,064

x-ref.test
Carroll #064

Would be easier with nawk, but this is what you asked for.

I should have known that I had done it before last year or something, it didn't work in the test script, but it did work in the main script -- odd?!?

Anyways, I will mess with it later, if anyone could help me with a new issue. The resultant string in the larger script has spaces and will not be assigned to the variable properly, such as:

Carroll #064

I can insert the preceding double-quotes, such as:

$MRK="Carroll #064

, but how do I use sed to append text. All of the man pages I have read for various systems say:

a\
`text'

where the quoted text gets appended to the line. But how do I steer the lines to be added to? Is there a place for a regular expression to be added here? I have been messing around with it for awhile now also and have not figured out how this part of sed works.

I have tried since posting this and found various ways to butcher the file, every line in the file, or simply erase the file completely. Always a good time. I tried to use sed also to search to the end of the line and append it with something like s/^\$MRK*$/\$MRK*"/. Of course it doesn't work but I like the frustration of experimentation -- NOT!

Most of what I have needed to do with sed is the typical s/// and / d type of stuff with various regular expressions.

Thank you `awk' for the help it was difficult getting it into the script because the line actually was writing into part of another script to be run.

I wrote the scope for the variable wrong, it should now be:

    HOLD=`grep -e "^$y," /x-ref | cut -f 2,3 -d , | sed -e 's/,/   #/`
    echo "MRK=\"$HOLD\""
    echo "echo \"Backup records for store: \$MRK\" >> \$TMP"

with an intermediary variable to hold the place for the on-the-fly-generated text in the created script. I removed the sed script for adding the quotes later on in the script and don't need to append anything. This is a lot easier solution, maybe I just needed sleep or something or a fresh look. Regardless to add this part for my boss added 4-5 seconds to the runtime for the script. It used to run in one or two seconds for the creation of the subservient script but that now moved to 7-8 seconds almost with this one line. Just FYI for those comparison shopping things to do in scripts and the time they take to run, the generated script has over 1000 lines in it.

Anyways if you happened to be kind enough to be working on an answer thanks anyways.

Hi I have a string variable

>Cat input.dat

This is a TEXT
This is a No

str="This is a TEXT"
cat input.dat | grep "$str" works fine in command line

but when i use it in shell script it identifies each space as a seperator and prints word by word.
How to suppress the space when searcing with GRep? Please help.
Thanks,
Mike