LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-31-2009, 12:46 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
redirecting to a temp file


Hi all,


Got struck with a logic. We used to store values in variables, which are stored in memory and not written to Hard disk.

say
Code:
my_varialbe=`ls -l |awk '{print $9}' |grep myfile`
I used to store a set of file names to a file and then recall them . There is a way to store the set of file names as it is and call them whenever needed . I want to store a file on memory and to recall it. How can I Do that?
say

Code:
`ls -l |awk '{print $9}' |read this to a file
will give a list of files and I just want to read it into a file and want to read the lines one by one , if needed.
How can I do that ?

Thanks
 
Old 01-31-2009, 03:12 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Your description is not really clear, this time. Can you elaborate a bit more, please? What do you mean for "read to a file"? Usually you can read FROM a file and write TO a file. I'm missing something. Anyway, if the problem is to read a file line by line, you should already know that a simple while loop does the trick:
Code:
while read line
do
  echo "$line"
done < file
 
Old 01-31-2009, 03:20 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
I agree with colucix (now you have both an Italian *and* an Aussie confused ... )
To write the data to a file, do you want something like this ???.
Code:
ls -l |awk '{print $9}' > outfile.txt
Then you can read it back later as per colucix post.
 
Old 01-31-2009, 03:25 AM   #4
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thanks Colucix

Here is an example of what am trying to do:

Code:
$ pwd
/etc/sysconfig
$ ls -l |awk '{print $9}'
auditd
authconfig
autofs
bluetooth
clock
console
crond
desktop
diskdump
dund
I want to store the above output as it is. so am doing
Code:
 ls -l |awk '{print $9}' >testfile
The testfile will be written to the disk. Instead I want to store it memory(without writing it to disk) and want to call the file lines one by one.

Is there a way?
 
Old 01-31-2009, 05:13 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe using something like process substitution?
Code:
while read fname
do
  echo "$fname"
done < <(ls -1)
In this way the output of ls -1 (note this is a -one not -el, which achieves the same result as your command) is processed in real time. Instead, if you want to store the list of files somewhere for later use, you can just assign the output of ls -1 to a variable and then reference that variable to retrieve the previously created list of files. For example:
Code:
my_list=$(ls -1)
<some other stuff here>
<and some time later...>
for fname in $(echo $my_list)
do
  echo "$fname"
done
 
Old 01-31-2009, 05:33 AM   #6
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Got that exactly. Thanks
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
redirecting stderr to file SPF Linux - Software 4 12-17-2007 06:56 AM
Temp file, where o where are you!? kReLiZ Linux - Desktop 7 12-08-2006 06:31 AM
Redirecting to file and stdout Quantum0726 Programming 3 11-14-2005 08:35 PM
why when redirecting output to a file any file extension seems to be fine? dr_zayus69 Linux - General 1 05-21-2005 04:09 AM
temp file, somewhere? florestan Linux - Software 9 04-14-2003 04:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:28 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration