FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 11-28-2009, 02:22 AM
Ray Parrish
 
Default Reading file into variable, and losing line feeds

Hello,

I am interested in being able to read the contents of a text file into a
variable, complete with line feeds.

Here is what I tried -

ray@RaysComputer:~/Bashscripts$ cat test.txt
Line 1
Line 2
Line 3
Line 4
Line 5
ray@RaysComputer:~/Bashscripts$ FileContents=`cat test.txt`
ray@RaysComputer:~/Bashscripts$ echo $FileContents
Line 1 Line 2 Line 3 Line 4 Line 5
ray@RaysComputer:~/Bashscripts$

Is there some trick to preserving the line feeds in the variable?

Thanks for any help you can be, 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
 
Old 11-28-2009, 06:41 AM
Nils Kassube
 
Default Reading file into variable, and losing line feeds

Ray Parrish wrote:
> I am interested in being able to read the contents of a text file
> into a variable, complete with line feeds.

> ray@RaysComputer:~/Bashscripts$ FileContents=`cat test.txt`
> ray@RaysComputer:~/Bashscripts$ echo $FileContents
> Line 1 Line 2 Line 3 Line 4 Line 5

> Is there some trick to preserving the line feeds in the variable?

The linefeeds are preserved but echo doesn't show them without quotes.
Try this:

echo "$FileContents"


Nils

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-28-2009, 01:47 PM
Ray Parrish
 
Default Reading file into variable, and losing line feeds

Nils Kassube wrote:
> Ray Parrish wrote:
>
>> I am interested in being able to read the contents of a text file
>> into a variable, complete with line feeds.
>>
>
>
>> ray@RaysComputer:~/Bashscripts$ FileContents=`cat test.txt`
>> ray@RaysComputer:~/Bashscripts$ echo $FileContents
>> Line 1 Line 2 Line 3 Line 4 Line 5
>>
>
>
>> Is there some trick to preserving the line feeds in the variable?
>>
>
> The linefeeds are preserved but echo doesn't show them without quotes.
> Try this:
>
> echo "$FileContents"
>
>
> Nils
>
Thank you Nils! I'm working on a multi-document, block oriented search,
and replace script to help me make broadcast changes across my web sites
all at one go.

None of the programmer's editors I installed would handle multi line
search, or replace terms, so I decided to write a program to do it for
me. Regexxer works fine if all you want to change is one line in all of
your files, but I need to make changes to multi line blocks of text like
adding a line in the middle of several other lines.

Now that I know the line feeds are still there, it will make coding the
script much easier.

Thanks again, 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
 

Thread Tools




All times are GMT. The time now is 12:53 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org