lorus77
asked on
Korn shell: conents of a file to a variable.
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.
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.
ASKER
sorry, no.
All it does is add $ perioidically where the endline character is suppose to be.
Eugene.
All it does is add $ perioidically where the endline character is suppose to be.
Eugene.
var=`cat -ve file.ext`
echo $var | perl -pe 's/\$( |$)/\n/g'
echo $var | perl -pe 's/\$( |$)/\n/g'
the variable itself cannot contain newlines and/or carriage returns
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Now why didn't I think of that? Duh.
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 `
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 `
Have you used double quotes?
var=`cat file.text`
echo "$var"
var=`cat file.text`
echo "$var"
oops, just seen JYoungmans answer... LOL :-)
See answer in my comment above.
try out this:
IFS=
var=`cat file`
echo $var
unset IFS
IFS=
var=`cat file`
echo $var
unset IFS
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 :-)
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 :-)
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.
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.
echo $var