Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Korn shell: conents of a file to a variable.

Posted on 2002-07-09
14
424 Views
Last Modified: 2013-11-18
Inside my korn shell there is a following pice of code:

var=`cat file.ext`
echo $var

The result are the contents of the file without the end lines. How do i make sure, that endline characters are included in var?

Thanks,

Eugene.
0
Comment
Question by:lorus77
  • 2
  • 2
  • 2
  • +8
14 Comments
 
LVL 6

Expert Comment

by:bira
ID: 7140951
var=`cat -ve file.ext`
echo $var
0
 

Author Comment

by:lorus77
ID: 7140993
sorry, no.

All it does is add $ perioidically where the endline character is suppose to be.

Eugene.
0
 
LVL 84

Expert Comment

by:ozo
ID: 7141063
var=`cat -ve file.ext`
echo $var | perl -pe 's/\$( |$)/\n/g'
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 7141320
the variable itself cannot contain newlines and/or carriage returns
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 7141336
Or, something more along the lines of

typeset -i n=0
cat file | while read LINE
do    var[n]="$LINE"
      n=$((n+1))
done


typeset -i m=0
while [ "$m" -lt "$n" ]
do      print -r -- "${var[$m]}"
        m=$((m+1))
done
0
 
LVL 2

Accepted Solution

by:
JYoungman earned 100 total points
ID: 7143015
Try

content="`cat file.ext`"
echo "$content"

or, better,

content="`cat file.ext`"
printf "%s\n" "$content"

0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 7143330
Now why didn't I think of that?  Duh.
0
 
LVL 2

Expert Comment

by:bedot
ID: 7146448
what utility to put an entiere file in an environment variable?
if you want absolutely this, you can add a newline with concatenation:  var2=$var'\n'

otherwise, is possible to put the command line in var:

var='cat file.text'

and then execute-it

$var

note: ' and not `
0
 
LVL 19

Expert Comment

by:jools
ID: 7167692
Have you used double quotes?

var=`cat file.text`
echo "$var"

0
 
LVL 19

Expert Comment

by:jools
ID: 7167693
oops, just seen JYoungmans answer... LOL :-)
0
 
LVL 2

Expert Comment

by:JYoungman
ID: 7168128
See answer in my comment above.
0
 
LVL 1

Expert Comment

by:Santunes
ID: 7473652
try out this:

IFS=
var=`cat file`
echo $var
unset IFS
0
 
LVL 20

Expert Comment

by:tfewster
ID: 7621426
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- Answered by JYoungman

Please leave any comments here before 13/1/2003

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster (I don't work here, I'm just an Expert :-)
0
 

Expert Comment

by:SpideyMod
ID: 7805318
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

JYoungman,
Please post all comments as comments, not as an answer unless you are certain your comment not only will solve the questioner's problem, but it is the only possible answer.  I realize it is frustrating when a questioner abandons a question, but you do have a course of action to rectify this.  Post a recommendation as tfewster did and then follow it up with a zero point question in community support and the moderators will take the appropriate action.  Thanks for your cooperation.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
OpenLDAP Proxy to Active Directy 6 365
Problem to picture file 3 81
Access_log 17 149
Randomize in Owl Carousel v1.3.2 6 68
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question