Unix Shell - Preserving newlines and carriage returns...

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2006 > Preserving newlines and carriage returns...





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Preserving newlines and carriage returns...
daraburke78@gmail.com

2006-02-26, 10:19 am

I'm importing a file from my local system into a bash script that I'm
running. Which will eventually use a curl function to post this file
contents onto the net. I need the contents of the file to be in both
form encoded and url encoded format.

The problem I'm having is with importing the file from the script. The
file contents comes in fine, but it looses it's linefeed and carriage
returns. I've tried replacing them with <br> and \n but to no avail, as
my two interent sites use htmlentities and stripslash php/asp
functions.

So how do I preserve newlines ? I need some other way to import the
files (rather than cat ) I think


cat job_description | while read file ; do php -r "echo
urlencode('$file');" ; done > job_description_encoded &&
URL_ENCODED_DESCRIPTION=`cat job_description_encoded`


URL_FORM_DESCRIPTION=`cat job_description`

Thanks,

Bill Marcum

2006-02-26, 10:19 am

On 24 Feb 2006 00:49:05 -0800, daraburke78@gmail.com
<daraburke78@gmail.com> wrote:
> I'm importing a file from my local system into a bash script that I'm
> running. Which will eventually use a curl function to post this file
> contents onto the net. I need the contents of the file to be in both
> form encoded and url encoded format.
>
> The problem I'm having is with importing the file from the script. The
> file contents comes in fine, but it looses it's linefeed and carriage
> returns. I've tried replacing them with <br> and \n but to no avail, as
> my two interent sites use htmlentities and stripslash php/asp
> functions.
>
> So how do I preserve newlines ? I need some other way to import the
> files (rather than cat ) I think
>
>
> cat job_description | while read file ; do php -r "echo
> urlencode('$file');" ; done > job_description_encoded &&


URL_ENCODED_DESCRIPTION="`cat job_description_encoded`"

URL_FORM_DESCRIPTION="`cat job_description`"


--
Never trust a child farther than you can throw it.
Chris F.A. Johnson

2006-02-26, 10:19 am

On 2006-02-24, daraburke78@gmail.com wrote:
> I'm importing a file from my local system into a bash script that I'm
> running. Which will eventually use a curl function to post this file
> contents onto the net. I need the contents of the file to be in both
> form encoded and url encoded format.
>
> The problem I'm having is with importing the file from the script. The
> file contents comes in fine, but it looses it's linefeed and carriage


I presume you mean "loses", not "looses".

> returns. I've tried replacing them with <br> and \n but to no avail, as
> my two interent sites use htmlentities and stripslash php/asp
> functions.
>
> So how do I preserve newlines ? I need some other way to import the
> files (rather than cat ) I think
>
> cat job_description | while read file ; do php -r "echo
> urlencode('$file');" ; done > job_description_encoded &&
> URL_ENCODED_DESCRIPTION=`cat job_description_encoded`
>
>
> URL_FORM_DESCRIPTION=`cat job_description`


In bash, you can do it without cat:

URL_FORM_DESCRIPTION=`< job_description`

You will lose trailing linefeeds, but carriage returns and internal
linefeeds will be preserved (as they will with cat).

If you want to keep trailing linefeeds:

URL_FORM_DESCRIPTION=`cat job_description; printf "."`
URL_FORM_DESCRIPTION=${URL_FORM_DESCRIPTION%?}


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
daraburke78@gmail.com

2006-02-27, 2:48 am

Hi guys,

thanks for the reply ... maybe it's just me - but I tried every one of
those scripts that you gave me and none of them preserved newlines in
my document.

Am I doing something wrong here ?

eg.

URL_FORM_DESCRIPTION=`< job_description`

echo $URL_FORM_DESCRIPTION

.... non-newline output

Thanks,

Dara

Chris F.A. Johnson wrote:
> On 2006-02-24, daraburke78@gmail.com wrote:
>
> I presume you mean "loses", not "looses".
>
>
> In bash, you can do it without cat:
>
> URL_FORM_DESCRIPTION=`< job_description`
>
> You will lose trailing linefeeds, but carriage returns and internal
> linefeeds will be preserved (as they will with cat).
>
> If you want to keep trailing linefeeds:
>
> URL_FORM_DESCRIPTION=`cat job_description; printf "."`
> URL_FORM_DESCRIPTION=${URL_FORM_DESCRIPTION%?}
>
>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence


Chris F.A. Johnson

2006-02-27, 2:48 am

On 2006-02-27, daraburke78@gmail.com wrote:
>
> Chris F.A. Johnson wrote:

[please don't top post]
[vbcol=seagreen]
> thanks for the reply ... maybe it's just me - but I tried every one of
> those scripts that you gave me and none of them preserved newlines in
> my document.
>
> Am I doing something wrong here ?
>
> eg.
>
> URL_FORM_DESCRIPTION=`< job_description`
>
> echo $URL_FORM_DESCRIPTION
>
> ... non-newline output


Quote the variable:

echo "$URL_FORM_DESCRIPTION"

Or:

printf "%s\n" "$URL_FORM_DESCRIPTION"

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
daraburke78@gmail.com

2006-02-27, 2:48 am


Chris F.A. Johnson wrote:
> On 2006-02-27, daraburke78@gmail.com wrote:
>
> [please don't top post]
>
>
> Quote the variable:
>
> echo "$URL_FORM_DESCRIPTION"
>
> Or:
>
> printf "%s\n" "$URL_FORM_DESCRIPTION"
>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence


Thanks chris,

Now the url_form_description thing works perfectly. But the url_encoded
seems to have gone off a bit. I've reworked it and replaced the while
read

So now I have this

URL_ENCODED_DESCRIPTION="`cat job_description`"

URL_ENCODED_DESCRIPTION=`php -r "echo
rawurlencode('$URL_ENCODED_DESCRIPTION')
;"`

echo "$URL_ENCODED_DESCRIPTION"

.... which gives me a url_encoded output with spaces and linefeeds, but
drops all newlines ... also ... the bash script seems to count any
apostraphe's in my file as variables and the rawurlencode() outputs
nasty T_STRING unexpected variables in my file.

Chris F.A. Johnson

2006-02-27, 8:48 pm

On 2006-02-27, daraburke78@gmail.com wrote:
> Chris F.A. Johnson wrote:
[snip][vbcol=seagreen]
>
> Thanks chris,
>
> Now the url_form_description thing works perfectly. But the url_encoded
> seems to have gone off a bit. I've reworked it and replaced the while
> read
>
> So now I have this
>
> URL_ENCODED_DESCRIPTION="`cat job_description`"
>
> URL_ENCODED_DESCRIPTION=`php -r "echo
> rawurlencode('$URL_ENCODED_DESCRIPTION')
;"`


Please don't let Google break up your lines; it can make it hard
to understand.

> echo "$URL_ENCODED_DESCRIPTION"
>
> ... which gives me a url_encoded output with spaces and linefeeds, but
> drops all newlines ...


Linefeeds and newlines are the same thing. What exactly is
happening?

> also ... the bash script seems to count any apostraphe's in my file
> as variables and the rawurlencode() outputs nasty T_STRING
> unexpected variables in my file.


First (and this is general advice, not only for this situation),
make sure that your command works without the command
substitution:

php -r "echo rawurlencode('$URL_ENCODED_DESCRIPTION')
;"

The URL encoding converts newlines to %0A. If you want to process
it line by line, you'll either have to find a way to code that in
php or feed it line at a time to php:

printf "%s\n" "$URL_ENCODED_DESCRIPTION" |
while IFS= read -r line
do
php -r "echo rawurlencode('$line');"
done

... in which case, you might as well read it directly from the
file.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com