The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep and assign it to variable xyz123 Shell Programming and Scripting 6 04-18-2008 05:12 AM
storing result of a command in variable eltinator Shell Programming and Scripting 1 04-17-2008 05:09 PM
assign value to variable using AWK HAA Shell Programming and Scripting 4 12-06-2006 08:43 AM
Setting a variable to result of FIND command JP Favara Windows & DOS: Issues & Discussions 2 03-09-2005 07:17 PM
Command param subst to reg expression videsh77 Shell Programming and Scripting 5 12-21-2004 11:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-14-2007
Registered User
 

Join Date: Nov 2007
Posts: 4
assign subst|grep|sed command result to a variable

Hi,
I'm quite new to scripting and I want to modify following line of an existing script:

MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`;

What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched.
Assuming I already have a variable MYSTRING which replaces the constant expression, how do I fit it into the above line to assign the final result of the whole piped command to MYVAR ?

thanks

-snow
Reply With Quote
Forum Sponsor
  #2  
Old 11-14-2007
Read Only
 

Join Date: Nov 2007
Posts: 165
Can you change 'L:\\\:' to "$MYSTRING" (note the change of single to double quotes)?
(Note that MYSTRING shouldn't have wildcards, and backslashes could get complicated.)
Reply With Quote
  #3  
Old 11-14-2007
Registered User
 

Join Date: Nov 2007
Posts: 4
Hi and thanks for replying.

I tried your suggestion but the command never returns.

if I echo $MYSTRING the result is M:\:

Is there another method by first constructing a string with the whole command and then using eval... to run it and assign the result to the final variable MYVAR ?

thanks again
Reply With Quote
  #4  
Old 11-14-2007
Read Only
 

Join Date: Nov 2007
Posts: 165
I hinted at the potential for problems the "\" in the string, and it looks like you may have hit that. Microsoft have a lot to answer for in choosing to use a backslash as the directory delimiter - it has cost man-years of effort in people having to unravel it (including within Microsoft, as they use C and its derivatives for development).
It is possible that the behaviour will differ depending upon whether you use the command in a script or at the command line, and whether you use single or double quotes.

I don't see why the command should've hung, so it just might be worth checking that you have matched up the pairs of single and double-quotes on the revised command.

It looks to me like you're trying find a line with a DOS path on it (grep 'L:\\\:' is looking for a string L:\:) and then strip off the path to leave the filename (sed -e 's/.*\\\//' means delete everything up to the last backslash).
If that is the case, then you might try replacing the hard-wired grep with an extended grep egrep '[A-Z]:\\\:'.

What's the output of the "subst" command alone?
And what would you like the result to look like?

Using eval is going to add in even more dependencies on the backslashes.

Incidentally, are you sure it's the right number of backslashes and : characters in your string?

Last edited by prowla; 11-14-2007 at 03:06 PM.
Reply With Quote
  #5  
Old 11-14-2007
Registered User
 

Join Date: Nov 2007
Posts: 4
a possible result of subst is e.g. following four lines:

M:\: => C:\subdir1\subdir11\subdir111
N:\: => D:\subdir2\subdir22\subdir222
W:\: => C:\subdir3\subdir33\subdir333
Z:\: => E:\subdir4\subdir44\subdir444

the whole command has to extract the deepest subdir from the line which starts with the string MYSTRING.
E.g. if MYSTRING=W:\: then the command should return subdir333 and assign it to MYVAR

I double-checked and the quote pairs are matching up and the string is correctly set with the exact number of backslashes.

It might be possible to grep for just e.g. M: instead of M:\: so I eliminate the unnecessary backslash complications, however I don't know how to change the format of the subsequent sed substitute command in that case...
I guess it wouldn't start with s/.* but something else.

Here the excerpt of the script as it is now:
.....
MYSTRING=`pwd |awk -F \/ '{print $3}'|tr '[a-z]' '[A-Z]'`
MYSTRING="$MYSTRING:\\:"
MYVAR=`subst |grep "$MYSTRING" | sed -e 's/.*\\\//'`;
.....

if I echo MYVAR it is empty.

The old script using a constant string as grep argument is working fine:
MYVAR=`subst |grep 'L\:\\\:' | sed -e 's/.*\\\//'`;

Last edited by snowbiker99; 11-14-2007 at 04:00 PM.
Reply With Quote
  #6  
Old 11-14-2007
Registered User
 

Join Date: Nov 2007
Posts: 4
ok now this finally works:

MYSTRING=`pwd |awk -F \/ '{print $3}'|tr '[a-z]' '[A-Z]'`
MYSTRING="$MYSTRING\:\\\:"
MYVAR=`subst |grep $MYSTRING | sed -e 's/.*\\\//'`;

basically I needed to escape everyone of the three characters :\: in the string and I removed the double quotes from the grep argument and just use $MYSTRING without quoting it

Thanks for the help
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0