character substitution
am 11.04.2008 18:12:02 von paul_0403
I have a data file, which is in name value format
cat data
var="c:\tmp\var" var2="c:\tmp\var1\var2"
I am trying to evaluate the variables but I would also like to convert
the windows slashes to UNIX style slashes.
I seem to be doing something wrong but I am not sure what, can
somebody help me out.
set -x
exec 6<"./data"
while read -u6 line
do
eval `print -r "$line" | tr -d " " | sed 's/\\\/\//g'`
done
exec 6<&-
Re: character substitution
am 11.04.2008 18:44:16 von Maxwell Lol
paul_0403@yahoo.com writes:
> I have a data file, which is in name value format
>
> cat data
> var="c:\tmp\var" var2="c:\tmp\var1\var2"
% echo 'var="c:\tmp\var" var2="c:\tmp\var1\var2"' | sed 's,\\,/,g'
var="c:/tmp/var" var2="c:/tmp/var1/var2"
Re: character substitution
am 11.04.2008 18:51:30 von PK
paul_0403@yahoo.com wrote:
> I have a data file, which is in name value format
>
> cat data
> var="c:\tmp\var" var2="c:\tmp\var1\var2"
>
> I am trying to evaluate the variables but I would also like to convert
> the windows slashes to UNIX style slashes.
>
> I seem to be doing something wrong but I am not sure what, can
> somebody help me out.
>
> set -x
> exec 6<"./data"
> while read -u6 line
> do
> eval `print -r "$line" | tr -d " " | sed 's/\\\/\//g'`
> done
> exec 6<&-
Assuming no spaces in the values between double quotes:
eval $(tr '\' '/' < data | \
sed 's%\([^[:space:]]*\)[[:space:]]*\([^[:space:]]*\)%\1;\2%')
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Re: character substitution
am 11.04.2008 19:13:02 von PK
pk wrote:
> Assuming no spaces in the values between double quotes:
>
> eval $(tr '\' '/' < data | \
> sed 's%\([^[:space:]]*\)[[:space:]]*\([^[:space:]]*\)%\1;\2%')
After some tests, it seems that multiple variable assignments per line are
allowed, even if not separated by ";". So.
eval $(tr '\' '/' < data)
should be all you need, assuming assignments are separated by whitespace and
spaces do not occur inside double quotes.
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Re: character substitution
am 11.04.2008 19:17:15 von PK
pk wrote:
> After some tests, it seems that multiple variable assignments per line are
> allowed, even if not separated by ";". So.
>
> eval $(tr '\' '/' < data)
>
> should be all you need, assuming assignments are separated by whitespace
> and spaces do not occur inside double quotes.
Correction: spaces /can/ appear inside double quotes, of course.
$ cat data
var="c:\tmp\var" var2="c:\tmp\var1\var2" var3="c:\foo\bar"
var4="c:\fgh" var5="c:\f o o\b a r"
$ eval $(tr '\' '/' < data)
$ echo "$var -- $var2 -- $var3 -- $var4 -- $var5"
c:/tmp/var -- c:/tmp/var1/var2 -- c:/foo/bar -- c:/fgh -- c:/f o o/b a r
Re: character substitution
am 11.04.2008 22:04:28 von Ed Morton
On 4/11/2008 11:12 AM, paul_0403@yahoo.com wrote:
> I have a data file, which is in name value format
>
> cat data
> var="c:\tmp\var" var2="c:\tmp\var1\var2"
>
> I am trying to evaluate the variables but I would also like to convert
> the windows slashes to UNIX style slashes.
>
> I seem to be doing something wrong but I am not sure what, can
> somebody help me out.
>
> set -x
> exec 6<"./data"
> while read -u6 line
> do
> eval `print -r "$line" | tr -d " " | sed 's/\\\/\//g'`
> done
> exec 6<&-
tr '\\' '/' < data > tmp &&
.. tmp &&
rm tmp
Ed.