LinuxQuestions.org
Help answer threads with 0 replies.
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, 01:46 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

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, 04:12 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 04:20 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,492

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
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, 04:25 AM   #4
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

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, 06:13 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 06:33 AM   #6
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 579

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


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 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 07:56 AM
Temp file, where o where are you!? kReLiZ Linux - Desktop 7 12-08-2006 07:31 AM
Redirecting to file and stdout Quantum0726 Programming 3 11-14-2005 09:35 PM
why when redirecting output to a file any file extension seems to be fine? dr_zayus69 Linux - General 1 05-21-2005 05:09 AM
temp file, somewhere? florestan Linux - Software 9 04-14-2003 05:20 PM


All times are GMT -5. The time now is 07:37 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration