Using variables in a regular expressions


 
Thread Tools Search this Thread
Operating Systems SCO Using variables in a regular expressions
# 1  
Old 04-10-2007
Data Using variables in a regular expressions

I have the code, for example:

Code:
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:
Code:
5,Carroll,064

Giving a result of "Carroll #064"

If I go out to a command-line and type:
Code:
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.

Smilie
# 2  
Old 04-10-2007
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.
# 3  
Old 04-11-2007
Power Thank You

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.

Last edited by PinkLemonade; 04-11-2007 at 12:01 PM..
# 4  
Old 04-12-2007
Bug I made a scope error on the variable

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

Code:
    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.
# 5  
Old 04-18-2007
Use a string with spaces as search pattern in Grep

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

2. UNIX for Advanced & Expert Users

Regular Expressions

Hi, below is a piece of code written by my predecessor at work. I'm kind of a newbie and am trying to figure out all the regular expressions in this piece of code. It is really a tough time for me to figure out all the regular expressions. Please shed some light on the regular expressions... (3 Replies)
Discussion started by: ramky79
3 Replies

3. UNIX for Dummies Questions & Answers

regular expressions

how to find for a file whose name has all characters in uppercase after 'project'? I tried this: find . -name 'project**.pdf' ./projectABC.pdf ./projectABC123.pdf I want only ./projectABC.pdf What is the regular expression that correponds to "all characters are capital"? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies

4. Shell Programming and Scripting

regular expressions

Hello, Let say I have a string with content "Free 100%". How can extract only "100" using ksh? I would this machanism to work if instead of "100" there is any kind of combination of numbers(ex. "32", "1238", "1"). I want to get only the digits. I have written something like this: ... (4 Replies)
Discussion started by: whatever
4 Replies

5. UNIX for Dummies Questions & Answers

regular expressions

Hi Gurus, I need help with regular expressions. I want to create a regular expression which will take only alpha-numeric characters for 7 characters long and will throw out an error if longer than that. i tried various combinations but couldn't get it, please help me how to get it guys. ... (2 Replies)
Discussion started by: ragha81
2 Replies

6. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

7. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies

8. UNIX for Dummies Questions & Answers

regular expressions variables in sed

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (1 Reply)
Discussion started by: Daniel234
1 Replies

9. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies

10. Shell Programming and Scripting

Regular Expressions

I'm trying to parse RichText to XML. I want to be able to capture everything between the '/par' tag in the RTF but not include the tag itself. So far all I have is this, '.*?\\par' but it leaves '\par' at the end of it. Any suggestions? (1 Reply)
Discussion started by: AresMedia
1 Replies
Login or Register to Ask a Question