Computing.Net > Forums > Unix > get data from file Linux

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

get data from file Linux

Reply to Message Icon

Name: ZeBen
Date: November 17, 2002 at 07:43:08 Pacific
OS: Win XP
CPU/Ram: 1GHz/256
Comment:

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)



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: November 17, 2002 at 09:05:19 Pacific
Reply:

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.


0

Response Number 2
Name: ZeBen
Date: November 17, 2002 at 12:23:32 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


SUID programs in /etc/pas... Extracting I-node details...



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: get data from file Linux

Extracting data from file using sed www.computing.net/answers/unix/extracting-data-from-file-using-sed/7660.html

Need help extracting data from file www.computing.net/answers/unix/need-help-extracting-data-from-file/7836.html

How to get data from mylog.log www.computing.net/answers/unix/how-to-get-data-from-myloglog/3805.html