Reading file into variable, and losing line feeds

Ray Parrish crp at cmc.net
Sat Nov 28 14:47:01 UTC 2009


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 at RaysComputer:~/Bashscripts$ FileContents=`cat test.txt`
>> ray at 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






More information about the ubuntu-users mailing list