Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Ubuntu User (http://www.linux-archive.org/ubuntu-user/)
-   -   Escaping quote marks (http://www.linux-archive.org/ubuntu-user/283627-escaping-quote-marks.html)

Ray Parrish 11-20-2009 11:24 AM

Escaping quote marks
 
Hello,

I have the following code which I would like to improve.

URLs=""
while read ThisLine; do
case "$ThisLine" in
*<loc>*) # if line starts with <loc> concatenate it
into string variable URLs.
URLs="$URLs $ThisLine";;
esac
done < "$MapName"
# Prompt user with drop down list of urls to select one to remove
from site map.
SelectedLine=`Xdialog --stdout --title "Add URL to Site Map -
Remove URL" --combobox "Select web page to remove from site map" 10 70
$URLs`

The problem is that if any of the iines starting with <loc> contain
spaces in their file names, the combobox presents them broken up at the
spaces on separate lines in it's drop down list.

I have tried to escape quote marks wrapped around each $ThisLine
concatenated to the URLs string variable as below to make them
individual parameters, but when I do this the quote marks get written to
the drop down list, and the entries are still broken at each space.

URLs="$URLs "$ThisLine"";;

Is there any way to get the code to work correctly so it does not break
the urls on space characters?

Thanks for any help you can be.

Later, Ray Parrish

--
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Ray Leventhal 11-20-2009 01:24 PM

Escaping quote marks
 
Ray Parrish wrote:
> Hello,
>
> I have the following code which I would like to improve.
>
> URLs=""
> while read ThisLine; do
> case "$ThisLine" in
> *<loc>*) # if line starts with <loc> concatenate it
> into string variable URLs.
> URLs="$URLs $ThisLine";;
> esac
> done < "$MapName"
> # Prompt user with drop down list of urls to select one to remove
> from site map.
> SelectedLine=`Xdialog --stdout --title "Add URL to Site Map -
> Remove URL" --combobox "Select web page to remove from site map" 10 70
> $URLs`
>
> The problem is that if any of the iines starting with <loc> contain
> spaces in their file names, the combobox presents them broken up at the
> spaces on separate lines in it's drop down list.
>
> I have tried to escape quote marks wrapped around each $ThisLine
> concatenated to the URLs string variable as below to make them
> individual parameters, but when I do this the quote marks get written to
> the drop down list, and the entries are still broken at each space.
>
> URLs="$URLs "$ThisLine"";;
>
> Is there any way to get the code to work correctly so it does not break
> the urls on space characters?
>
> Thanks for any help you can be.
>
> Later, Ray Parrish
>

Hi Ray,

I'm not sure if this will work, but does escaping the space characters
have the desired effect?

i.e.: URLs="$URLs "$ThisLine"";;

or

URLs="$URLs $ThisLine";;

HTH,
-Ray

--
================================================== ===================
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
================================================== ===================
() ascii ribbon campaign - against html e-mail
/ www.asciiribbon.org - against proprietary attachments

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Nils Kassube 11-20-2009 01:24 PM

Escaping quote marks
 
Ray Parrish wrote:
> Is there any way to get the code to work correctly so it does not
> break the urls on space characters?

Maybe replace each space character of the URL with "%20"?


Nils

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Ray Parrish 11-20-2009 06:57 PM

Escaping quote marks
 
Nils Kassube wrote:
> Ray Parrish wrote:
>
>> Is there any way to get the code to work correctly so it does not
>> break the urls on space characters?
>>
>
> Maybe replace each space character of the URL with "%20"?
>
>
> Nils
>
Hello,

I have already manually edited the site map file, and replaced all of
the space characters with %20, but I would like to know how to handle
the spaces in my script so that doesn't have to be done.

I'm releasing the script as open source software, and it would be nice
if I did not have to tell my users they need to manually replace all of
the space characters with %20 in their site maps.

Ahhh, it just dawned on me... I bet I can replace the space characters
with %20 as I load each line in from the file. I have no idea how to do
that, as bash is not real string handling friendly, but I will work on
it some, and see if I can figure out how to.

Later, Ray Parrish


--
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Aart Koelewijn 11-20-2009 07:36 PM

Escaping quote marks
 
On Fri, 20 Nov 2009 11:57:54 -0800, Ray Parrish wrote:

> Nils Kassube wrote:
>> Ray Parrish wrote:
>>
>>> Is there any way to get the code to work correctly so it does not
>>> break the urls on space characters?
>>>
>>>
>> Maybe replace each space character of the URL with "%20"?
>>
>>
>> Nils
>>
> Hello,
>
> I have already manually edited the site map file, and replaced all of
> the space characters with %20, but I would like to know how to handle
> the spaces in my script so that doesn't have to be done.
>
> I'm releasing the script as open source software, and it would be nice
> if I did not have to tell my users they need to manually replace all of
> the space characters with %20 in their site maps.
>
> Ahhh, it just dawned on me... I bet I can replace the space characters
> with %20 as I load each line in from the file. I have no idea how to do
> that, as bash is not real string handling friendly, but I will work on
> it some, and see if I can figure out how to.
>
> Later, Ray Parrish

In bash I would use sed for that. "sed s/ /%20/g" from the top of my
head. You might have to do some escaping. You can first transform the
whole file and then pass it trough the rest off your script, but it is
also possible to do it line by line, I think. Read up on it, sed is a
rather powerfull tool, together with awk I have used it a lot over the
years. But you can also use perl off course for all off your script. It
is what I do when things get a bit complicated for bash, sed and awk.

--
Aart


--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Nils Kassube 11-20-2009 08:04 PM

Escaping quote marks
 
Ray Parrish wrote:
> Nils Kassube wrote:
> > Maybe replace each space character of the URL with "%20"?

> Ahhh, it just dawned on me... I bet I can replace the space
> characters with %20 as I load each line in from the file. I have no
> idea how to do that, as bash is not real string handling friendly,
> but I will work on it some, and see if I can figure out how to.

Is this what you are looking for?

URLs=""
while read ThisLine; do
case "$ThisLine" in
*<loc>*)
while test "$ThisLine" != "${ThisLine#* }";do
x="${ThisLine#* }"
y="${ThisLine%$x}"
ThisLine="${y% }%20$x"
done
URLs="$URLs $ThisLine"
;;
esac
done < "$MapName"


Nils

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Cameron Hutchison 11-20-2009 09:54 PM

Escaping quote marks
 
Ray Parrish <crp@cmc.net> writes:

>Hello,

>I have the following code which I would like to improve.

> URLs=""
> while read ThisLine; do
> case "$ThisLine" in
> *<loc>*) # if line starts with <loc> concatenate it
>into string variable URLs.
> URLs="$URLs $ThisLine";;
> esac
> done < "$MapName"
> # Prompt user with drop down list of urls to select one to remove
>from site map.
> SelectedLine=`Xdialog --stdout --title "Add URL to Site Map -
>Remove URL" --combobox "Select web page to remove from site map" 10 70
>$URLs`

>The problem is that if any of the iines starting with <loc> contain
>spaces in their file names, the combobox presents them broken up at the
>spaces on separate lines in it's drop down list.

If you are using bash, then you can use arrays to hold the URLs such
that each URL can contain spaces and you can still distingish individual
elements.

Arrays allow you to use out-of-band separators between elements, instead
of trying to come up with a character that can be used to separate
elements and hoping that the character does not appear in your data.

Something like this (untested):

#!/bin/bash

URLs=()
while read ThisLine ; do
case "$ThisLine" in
*<loc>*)
URLs=("${URLs[@]}" "$ThisLine") ;;
esac
done < "$MapName"

SelectedLine=$(Xdialog --stdout --title "Add URL to Site Map - Remove URL" --combobox "Select web page to remove from site map" 10 70 "${URLs[@]}")


--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

James Michael Fultz 11-20-2009 10:01 PM

Escaping quote marks
 
* Ray Parrish <crp@cmc.net> [2009-11-20 04:24 -0800]:
> I have the following code which I would like to improve.
>
> URLs=""
> while read ThisLine; do
> case "$ThisLine" in
> *<loc>*) # if line starts with <loc> concatenate it
> into string variable URLs.
> URLs="$URLs $ThisLine";;
> esac
> done < "$MapName"
> # Prompt user with drop down list of urls to select one to remove
> from site map.
> SelectedLine=`Xdialog --stdout --title "Add URL to Site Map -
> Remove URL" --combobox "Select web page to remove from site map" 10 70
> $URLs`
>
> The problem is that if any of the iines starting with <loc> contain
> spaces in their file names, the combobox presents them broken up at the
> spaces on separate lines in it's drop down list.
>
> I have tried to escape quote marks wrapped around each $ThisLine
> concatenated to the URLs string variable as below to make them
> individual parameters, but when I do this the quote marks get written to
> the drop down list, and the entries are still broken at each space.
>
> URLs="$URLs "$ThisLine"";;
>
> Is there any way to get the code to work correctly so it does not break
> the urls on space characters?

read doesn't just read in a line by default -- it splits lines into
fields based on the value of $IFS which defaults to space, tab, and
newline. Setting $IFS to a null value will cause read to read in
a complete line. The '-r' option is also recommended to preserve
embedded backslash characters.

while IFS= read -r ThisLine; do

In a later message, you make mention of Bash. If you are in fact using
Bash for your script, an array for $URLs would be better for your
purposes.

URLs=()
while IFS= read -r ThisLine; do
case $ThisLine in
*'<loc>'*)
URLs+=( $ThisLine )
;;
esac
done < "$MapName"
SelectedLine=$(Xdialog --stdout
--title "Add URL to Site Map - Remove URL"
--combobox "Select web page to remove from site map"
10 70 "${URLs[@]}")

Also, you could use Bash parameter expansion to replace spaces with %20.

case $ThisLine in
*'<loc>'*)
url=${ThisLine// /%20}
URLs+=( $url )
;;
esac

Alternatively, the following would work in any POSIX shell (/bin/sh).

url=$(echo "$ThisLine" | sed 's/ /%20/g')

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Ray Parrish 11-20-2009 11:56 PM

Escaping quote marks
 
Nils Kassube wrote:
> Ray Parrish wrote:
>
>> Nils Kassube wrote:
>>
>>> Maybe replace each space character of the URL with "%20"?
>>>
>
>
>> Ahhh, it just dawned on me... I bet I can replace the space
>> characters with %20 as I load each line in from the file. I have no
>> idea how to do that, as bash is not real string handling friendly,
>> but I will work on it some, and see if I can figure out how to.
>>
>
> Is this what you are looking for?
>
> URLs=""
> while read ThisLine; do
> case "$ThisLine" in
> *<loc>*)
> while test "$ThisLine" != "${ThisLine#* }";do
> x="${ThisLine#* }"
> y="${ThisLine%$x}"
> ThisLine="${y% }%20$x"
> done
> URLs="$URLs $ThisLine"
> ;;
> esac
> done < "$MapName"
>
>
> Nils
>
Wow Nils, now if I could just understand what your code is actually
doing. I did solve the problem with a function and word expansion. I
pass each line to a function that receives the line as a parameter. If
there are spaces in it, it gets treated like several command line
parameters.

Here's my code. -

function ReplaceSpaces {
# loop through command line parameters, concatenating them with %20
# Assign the value of $1 to ThisLine
ThisLine="$1"
# shift command line parameters to the next one
shift
# if the command line arguments did not expire after the first
shift, there must be spaces
while [ "$1" != "" ]
do
# Concatenate ThisLine with %20, and the next command line
argument
ThisLine="$ThisLine%20$1"
# shift to the next command line argument
shift
done
return
}

And it gets called like so -

ReplaceSpaces $ThisLine

It works great, now all spaces in urls within my Google site map get
replaced with %20, and the script runs just fine now.

Later, Ray Parrish

--
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

Nils Kassube 11-21-2009 06:20 AM

Escaping quote marks
 
Ray Parrish wrote:
> Wow Nils, now if I could just understand what your code is actually
> doing.

Here's an explanation:

> > while test "$ThisLine" != "${ThisLine#* }";do
We loop until "$ThisLine" is the same as "$ThisLine" with the first part
of the string including the first space is removed, i.e. until there is
no space in the string. We replace one space at a time, so we need a
loop.

> > x="${ThisLine#* }"
"$x" becomes the end of the string after the first space.

> > y="${ThisLine%$x}"
"$y" becomes the start of the string with "$x" removed at the end, i.e.
the start of the string up to (and including) the first space.

> > ThisLine="${y% }%20$x"
Concatenate "$y" without the space at the end, "%20" and "$x".

> I did solve the problem with a function and word expansion. I
> pass each line to a function that receives the line as a parameter.
> If there are spaces in it, it gets treated like several command line
> parameters.

That will probably work here because it is unlikely that your URLs have
several spaces in sequence. However if you recycle your code (like my
code above was taken from a script I wrote long ago) it may not work in
the new context.


Nils

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users


All times are GMT. The time now is 04:01 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.