Stripping out value from file, value is a variable though.

Stripping out value from file, value is a variable though.

Post by Tony Higgin » Thu, 30 Nov 2000 04:00:00



In sh I am trying to assign a variable the following value:

dest=/users/name

echo $dest | sed 's/\//\\\//g'

comes up with \/users\/name

Which I want ot assign to a variable name, I have tried:

dest_new='echo $dest_dir | sed 's/\//\\\//g''
dest_new=`echo $dest_dir | sed 's/\//\\\//g'`
dest_new="echo $dest_dir | sed 's/\//\\\//g'"
eval dest_new="echo $dest_dir | sed 's/\//\\\//g'"
eval dest_new=`echo $dest_dir | sed 's/\//\\\//g'`
eval dest_new='echo $dest_dir | sed 's/\//\\\//g''

None of them work!

For a full picture of the entire problem please read below, someone may
know a quicker overall way rather that helping me out on this piece of
the puzzle.

Problem:
1) Read in a path eg /users/name/bin
2) find path -print -> file
3) Strip out the first part of each line in the file which is the $path,
not $path can change each time.

so

/users/name/bin/1
/users/name/bin/exe
/users/name/bin/several
/users/name/bin/files
/users/name/bin/down/here

Would become
1
exe
several
files
down/here

In theory you should be able to do something like:

cat $file | sed 's/$dest//g'

replacing the previous read path with nothing!!

Thanks

--
Tony

 
 
 

Stripping out value from file, value is a variable though.

Post by Tony Higgin » Thu, 30 Nov 2000 04:00:00


As always Baz, you're a superstar. This should get me going.

Thanks




> >In sh I am trying to assign a variable the following value:

> >dest=/users/name

> >echo $dest | sed 's/\//\\\//g'

> >comes up with \/users\/name

> >Which I want ot assign to a variable name, I have tried:

> >dest_new='echo $dest_dir | sed 's/\//\\\//g''
> >dest_new=`echo $dest_dir | sed 's/\//\\\//g'`
> >dest_new="echo $dest_dir | sed 's/\//\\\//g'"
> >eval dest_new="echo $dest_dir | sed 's/\//\\\//g'"
> >eval dest_new=`echo $dest_dir | sed 's/\//\\\//g'`
> >eval dest_new='echo $dest_dir | sed 's/\//\\\//g''

> dest_new=`echo $dest | sed 's/\\//\\\\\\//g'`

> Backticks eat unescaped slashes, so you have to double them to get them
> through to sed.

> >In theory you should be able to do something like:

> >cat $file | sed 's/$dest//g'

> You have to use doublequotes here, otherwise $dest won't be substituted.

> Another thing you can do is use a different delimiter character with sed.
> If there's a character you know will never appear in $dest, e.g. #, you can
> write:

> sed "s#$dest##g" $file

> BTW, are you sure you want to use the "g" modifier?  If this is just
> supposed to delete a path prefix, why would you want to delete it multiple
> times on the line?  It should probably be:

> sed "s#^$dest##" $file

> --

> Genuity, Burlington, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

--
Tony

 
 
 

Stripping out value from file, value is a variable though.

Post by Barry Margoli » Fri, 01 Dec 2000 09:27:58




Quote:>In sh I am trying to assign a variable the following value:

>dest=/users/name

>echo $dest | sed 's/\//\\\//g'

>comes up with \/users\/name

>Which I want ot assign to a variable name, I have tried:

>dest_new='echo $dest_dir | sed 's/\//\\\//g''
>dest_new=`echo $dest_dir | sed 's/\//\\\//g'`
>dest_new="echo $dest_dir | sed 's/\//\\\//g'"
>eval dest_new="echo $dest_dir | sed 's/\//\\\//g'"
>eval dest_new=`echo $dest_dir | sed 's/\//\\\//g'`
>eval dest_new='echo $dest_dir | sed 's/\//\\\//g''

dest_new=`echo $dest | sed 's/\\//\\\\\\//g'`

Backticks eat unescaped slashes, so you have to double them to get them
through to sed.

Quote:>In theory you should be able to do something like:

>cat $file | sed 's/$dest//g'

You have to use doublequotes here, otherwise $dest won't be substituted.

Another thing you can do is use a different delimiter character with sed.
If there's a character you know will never appear in $dest, e.g. #, you can
write:

sed "s#$dest##g" $file

BTW, are you sure you want to use the "g" modifier?  If this is just
supposed to delete a path prefix, why would you want to delete it multiple
times on the line?  It should probably be:

sed "s#^$dest##" $file

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.