Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi to all
I am desesperaly trying to get the data from a file with shell (Linux/Unix).
It is a init file with this structure:"init.dat"
10. # x0 - initial position X
-2. # y0 - initial position Y
0.02 # t0 - initial time
...and trying to put 10. in $x0, -2. in $y0...
I have tried to use 'sed', 'awk', even loops.. But could not do it..
Can anyone Help?
Thanx a lot
B.
(sorry for my english.. it is not my native language)

One solution is:
set `cat file | cut -f1 -d'#'`
x0=$1
y0=$2
t0=$3
The commands "cat init.dat | cut -f1 -d'#'"will return
10.
-2.
0.02
In the cut command, -f1 say to return field 1 and the -d'#' says that fields are separated (delimited) by the # character.Then add the set command:
set `cat file | cut -f1 -d'#'`
(be sure that you use backquotes around the cat....-d'#' part.
The backquotes cause the output of the quoted commands to be passed as parameters to the set command, which takes those values and puts them in variables $1, $2..., which you transfer to the variables that you want, or just use $1, $2 etc.

Thank you soo much !!
I think my main problem was to use 'set' properly...
Your technique works perfectly..But because I was upset not to have succeeded it with awk, I have re-used your script, noticing where 'set' was, and tried again, just for fun, to do it with 'awk':
set `awk '{print $1}' init.dat`
x0=$1
y0=$2
...And this also works..
Thanks again..
ZeBen

![]() |
SUID programs in /etc/pas...
|
Extracting I-node details...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |