assign subst|grep|sed command result to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign subst|grep|sed command result to a variable
# 1  
Old 11-14-2007
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
# 2  
Old 11-14-2007
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.)
# 3  
Old 11-14-2007
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
# 4  
Old 11-14-2007
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 06:06 PM..
# 5  
Old 11-14-2007
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 07:00 PM..
# 6  
Old 11-14-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove a character and assign result to a variable

I am reading lines from a file that contain a number sign (#) before a three or four digit number: #1043 #677 I can remove the '#' and get just the number. However, I then want to assign that number to a variable and use it as part of a path further on in my program: /mydir/10/1043 for... (5 Replies)
Discussion started by: KathyB148
5 Replies

2. Shell Programming and Scripting

Assign awk gsub result to a variable

Hello, I have searched but failed to find what exactly im looking for, I need to eliminate first "." in a output so i can use something like the following echo "./abc/20141127" | nawk '{gsub("^.","");print}' what i want is to use gsub result later on, how could i achieve it? Let say... (4 Replies)
Discussion started by: EAGL€
4 Replies

3. Shell Programming and Scripting

If grep value is null then assign 0 to variable

DELETE=`cat $logfile1 | egrep -i "Delete" | sed 's/ */ /g' | cut -d" " -f2` INSERT=`cat $logfile1 | egrep -i "Insert" | sed 's/ */ /g' | cut -d" " -f2` UPDATE=`cat $logfile1 | egrep -i "Update" | sed 's/ */ /g' | cut -d" " -f2` I need soming like below: if value is null... (8 Replies)
Discussion started by: Veera_V
8 Replies

4. Shell Programming and Scripting

Assign the result of a multiline command to a variable

Hi, I have the following command that lists all the .o files from all the directories except of vwin (which I don't want it) for i in `ls -d */*.o|awk '$0 !~ "vwin"'`; do echo $i; done The result is something like that dir1/file1.o dir1/file2.o dir2/file3.o etc. So, I want to create a... (9 Replies)
Discussion started by: scor6800
9 Replies

5. Shell Programming and Scripting

Assign grep match to variable question

Hi, I'm trying to assign a grep result to a variable but instead of having all grep result's assigned to the variable, would it be possible to assign the first match, do something, then move onto the next match and assign it to that variable and so on until all matches have been completed I... (4 Replies)
Discussion started by: Jazmania
4 Replies

6. UNIX for Dummies Questions & Answers

Assign SQL result in shell variable

Hi im trying to assign the result of the db2 command to a variable inside a shell script... : tab_cnt=`db2 "select count(*) from syscat.tables where tabname = 'ABC' and tabschema = 'MATT01'" |head -4|tail +4|cut -c 11` : echo $tab_cnt when i echo im getting a blank value.. im expecting... (1 Reply)
Discussion started by: matt01
1 Replies

7. Shell Programming and Scripting

Assign result to variable

Hi friends, firstly, i can run following expression and i took 100 value. sqlplus -s username/password@TTTEST @umt.sql umt.sql exists "select t.deger from parametre t where t.id=30". result of this query =100 i need to assign this value(100) to variable(for example x... (2 Replies)
Discussion started by: temhem
2 Replies

8. Shell Programming and Scripting

How to assign the result of a SQL command to more than one variable in shell script.

Hi Friends... Please assist me to assign the result of a SQL query that results two column, to two variables. Pls find the below code that I write for assigning one column to one variable. and please correct if anything wrong.. #! /bin/sh no=' sqlplus -s uname/password@DBname... (4 Replies)
Discussion started by: little_wonder
4 Replies

9. Shell Programming and Scripting

assign awk command result to a variable

#!/bin/sh # ## MYSTRING = `awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist` if then echo String not found defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string... (9 Replies)
Discussion started by: dedmakar
9 Replies

10. Shell Programming and Scripting

grep and assign it to variable

Hi , Help required i want to grep a pattern from a file using "grep -n" command then cut the field (i.e line number return by cut command) and assign it to a variable e.g var=grep -n "end" FILE1 | cut -f1 -d":" But i am not able to perform this operation. i am performing all... (6 Replies)
Discussion started by: xyz123
6 Replies
Login or Register to Ask a Question