setting variable value to dynamic sed match - escaping hell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting setting variable value to dynamic sed match - escaping hell
# 1  
Old 11-04-2010
Question setting variable value to dynamic sed match - escaping hell

Hello All,

I'm trying to write a script that will perform a dynamic match (of a dynamic variable) and set a variable to have the resulting (match) value.

The idea is that the environment variable to check ($1) and the regular expression to use ($2) are given as parameters.

For example, to print the user's username only if it begins with an 'a', we would make the call:
Code:
check-var.sh USER "^a.*$"

I've tried doing this:
Code:
VAR_NAME=$1
 
VAR_REGEXP=$2
 
VAR_MATCH=`eval "echo \$${VAR_NAME} | sed -n \"/$VAR_REGEXP/p\""
 
echo $VAR_MATCH

But there seems to be a problem with by quoting and escaping because if I execute the eval directly like this:
Code:
eval "echo \$${VAR_NAME} | sed -n \"/$VAR_REGEXP/p\""

the expected result is printed.

Anyone out there have an idea of how I can capture the result in the VAR_MATCH variable? I ask because I actually need to work with the matched value later on in the script and not simply send it to stdout.

Thanks,
- Andy

Last edited by Scott; 11-04-2010 at 07:20 AM.. Reason: Please use code tags
# 2  
Old 11-04-2010
If you start the script with:
Code:
check-var.sh USER "^a.*$"

you can get the result in your script with:
Code:
VAR_MATCH=$(echo $1 | grep $2)

echo $VAR_MATCH

Or am I missing something?
This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 11-04-2010
Quote:
Originally Posted by aedgar
Hello All,
Code:
VAR_MATCH=`eval "echo \$${VAR_NAME} | sed -n \"/$VAR_REGEXP/p\""

But there seems to be a problem with by quoting and escaping

Thanks,
- Andy
Looks like you missed the closing "`".
# 4  
Old 11-04-2010
Further to majormark.

We need more backslash characters to properly escape the eval statement when run from within a shell script.
Also I had to export $USER before invoking the script.

Code:
VAR_MATCH=`eval "echo \\$\${VAR_NAME} | sed -n \"/${VAR_REGEXP}/p\""`

# 5  
Old 11-04-2010
Quote:
Originally Posted by Franklin52
If you start the script with:
Code:
check-var.sh USER "^a.*$"

you can get the result in your script with:
Code:
VAR_MATCH=$(echo $1 | grep $2)
 
echo $VAR_MATCH

Or am I missing something?
Thanks for quick answer; you put me on the right track.

What I am actually trying to do to check the *value* for the variable named in $1 and not the variable name itself.

What did the trick was:
Code:
VAR_MATCH=$(eval echo \$${VAR_NAME} | grep -e "$VAR_REGEXP")

Thanks for your help,
- Andy
# 6  
Old 11-04-2010
Just for interest, we can do this without "eval".
Code:
VAR_MATCH=$(set | grep \^"${VAR_NAME}=${VAR_REGEXP}")

We must not have a "^" at the start of $2 for this to work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escaping a variable in ksh

I am trying to echo a variable exactly to a script- echo "${var1} ${var2} >> output.output Gives me a blank file. I would like output.output to basically say: ${var1} ${var2} I think I need to use a special escape character for variables. Am I right in assuming that, and is it the... (8 Replies)
Discussion started by: jeffs42885
8 Replies

2. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

3. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

4. Shell Programming and Scripting

Sed:- Supported variable replacement after string match?

Hi All, I am trying to replace the variable in the file after the particular match string. It is being replaced if i hardcode the value and with use of "&" with sed. sed -e "s/URL./& http:\\localhost:7223/g" But when am trying to pass the variable it is failing. I tried multiple... (9 Replies)
Discussion started by: sharsour
9 Replies

5. Shell Programming and Scripting

Escaping backslash and asterisk in egrep to match "\*"

So far what i've got is egrep '^(\\)\*$'No luck. I've searched the web and not much luck. I know about the escape character \ but its confusing to figure out how to use it to match a backslash and use it to escape the asterisk also. Any ides? Thanks! (8 Replies)
Discussion started by: matthewfs
8 Replies

6. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

7. UNIX for Dummies Questions & Answers

Escaping " and ' in sed

Okay, I have looked through everything and cant find it... I need to append a line to the end of a a file: I can do it like this: sed '$ a\additional line' -i file.txt However, the extra line I need has both " and ' in it: ie, "additional" line's has to be added. sed '$... (2 Replies)
Discussion started by: imrikk
2 Replies

8. UNIX for Dummies Questions & Answers

Escaping non-readable characters using grep, sed or awk

I'm trying to parse out DNS logs from dozens of different domain controllers over a large period of time. The logs are rolled up into individual text files by size, which may contain only a portion of a day's activity or several day's worth (depending on amount of activity). I'm splitting them by... (4 Replies)
Discussion started by: seanwpaul
4 Replies

9. UNIX for Advanced & Expert Users

dynamic match thru awk

hey , my i/p text looks like this, FILE_TYPE=01|FILE_DESC=Periodic|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=B FILE_TYPE=02|FILE_DESC=NCTO|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=M NOTE Look carefully for the position FILE_TYPE,FILE_DESC... (23 Replies)
Discussion started by: manas_ranjan
23 Replies

10. IP Networking

Setting up BIND with dynamic IP

I'm a UNIX newbie, so I know I'll be making foolish errors here... I'm trying to set up my Mac OS X 10.2 server to serve my domain name. I've got Apache running reasonably well , and I know Sendmail is up and running... Now, I seem to need to get BIND running for the domain name, and it... (2 Replies)
Discussion started by: caphector
2 Replies
Login or Register to Ask a Question