LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-01-2005, 08:57 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
how to cat a text file and save it as a variable


I have a text file that needs to be read, printed, and then saved as a variable. I know that to read and print a file I would use:

cat filename

But after it is read and printed, how would I save it as a variable. To give everyone a bit more insight on what I am trying to do. I have a text file that is overwritten every second...it looks like this

10
103
45
67

These four values are overwrote by new values every second. I need to find a function to save this as a variable and read the new text file evey second. How would I do this?
 
Old 07-01-2005, 09:12 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Try this....

file:

Code:
$ cat 1
10
103
45
67
use the 'set' command:

Code:
while sleep 1 ; do  
 set -- $( cat 1)
 echo "$1 $2 $3 $4"
 done

10 103 45 67
10 103 45 67
10 103 45 67

etc... etc
 
Old 07-01-2005, 09:18 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Code:
for i in 0 1 2 3 4 5 6 7 8 9 10
do
  a[$i]="$(<filename)"
  echo "${a[$i]}"
  sleep 1
done

echo "${a[5]}"
echo "${a[10]}"
 
Old 07-01-2005, 09:21 AM   #4
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Bigearsbilly,

Will this be true if the numbers change? The numbers that I gave in the example will not ever be the same....they are always changing. How would I account for changing numbers?
 
Old 07-01-2005, 09:24 AM   #5
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
jlliarge,

I dont understand the:

for i in 0 1 2 3 4 5 6 7 8 9 10

Could you explain your code better...I am new to shell coding and do not understand what you are tryinging to do. It looks like your setting up an array, but this program will run all day everyday and a new textfile will be generated ever second of the day, therefore; I would not want an array just some type of loop that will constantly read and print out the text file and only stop on a command.
 
Old 07-01-2005, 09:26 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
of course.
it cats the file every second.
 
Old 07-01-2005, 09:27 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Well, you asked for storing the file content in a variable, so I indeed created an array for these variable to be persistants.

if you need not to save the values, you probably neither need a variable:

Code:
while true
do
  cat filename
  sleep 1
done
 
Old 07-01-2005, 09:41 AM   #8
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
how about:
Code:
foo="`cat filename`"
echo "$foo"
and do the looping part however you like, of course

Last edited by aluser; 07-01-2005 at 09:42 AM.
 
Old 07-01-2005, 09:46 AM   #9
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
jlliagre,

You have me confused now and I am sorry for that. I just dont understand what I need to do. First you told me to use:

Code:
for i in 0 1 2 3 4 5 6 7 8 9 10
do
  a[$i]="$(<filename)"
  echo "${a[$i]}"
  sleep 1
done

echo "${a[5]}"
echo "${a[10]}"
What is the significants of 5 and 10 and why is the array 0-10. You print in the loop and out of the loop. What are you printing each time?
Then you tell me

Code:
while true
do
  cat filename
  sleep 1
done
What will this code do?
 
Old 07-01-2005, 09:51 AM   #10
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
aluser,

If I did something like this:

Code:
while true
do
  foo="`cat filename`"
  echo "$foo"
  sleep 1
done
Would I get the following functionality:

1) Whatever is in filename will be read and set to the variable foo
2) I print foo to the screen
3) Wait and then repeat.........read....set equal to foo.....print......read....set equal to foo.....print..etc

If so what will the done do and how would I ever get out of this loop if I needed to....or is there a better way to structure my loop?
 
Old 07-01-2005, 09:52 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
My sample code is doing what I understood about your requirements.

Precisely storing every second the content of a file in a variable.

Why is the array 0 to 10 ?

why not ? it is just an example.

What is the significants of 5 and 10 ?

Just to show the sixth and eleventh value stored can be retrieved after the loop is ended.

The second code is displaying the content of the file "filename" every second, forever.

This seems to be what you really want, but please clarify your requirements if I'm mistaken.
 
Old 07-01-2005, 09:55 AM   #12
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
that loop is infinite. You can always hit control-C on your terminal to kill it. If you want some other terminating condition, tell us what it should be : )


The "done" just tells the shell that's where the bottom of the loop is; stuff below done won't be included in the loop. (The shell doesn't know what you mean by indenting the body of the loop; you just do that so it's more readable to a human)
 
Old 07-01-2005, 10:09 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
In the following code, the "foo" variable is totally useless.

Code:
while true
do
  foo="`cat filename`"
  echo "$foo"
  sleep 1
done
This code is strictly equivalent to that one:

Code:
while true
do
  cat filename
  sleep 1
done
or even the smarter:

Code:
while sleep 1
do
  cat filename
done
 
Old 07-01-2005, 10:13 AM   #14
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
jlliarge,

Do you think this is the best way to go for what I am trying to do?

Code:
while sleep 1
do
  cat filename
done
 
Old 07-01-2005, 10:16 AM   #15
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
well, what are you trying to do? I think that's the problem here : )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
trying to redirect text to a file to cat at later point. says file doesn't exist. dr_zayus69 Programming 1 10-02-2005 08:10 AM
NEED AWNSER QUICK how do I save editing a text file in Konsole? DDRfreak2 Mandriva 1 08-18-2005 06:09 PM
wiring to a text file and being able to save and add to it mrobertson Programming 1 07-27-2005 02:27 PM
Find variable in template file replace w/date+ and save as jmanjohn61 Linux - General 14 12-13-2004 06:49 AM


All times are GMT -5. The time now is 04:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration