ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a file called test.txt which I would like to read line by line in a while loop and store the contents of every line in a variable, do some processing and again loop back to the next line until I reach at the end of the file(test.txt) through a shell script.
Basically, here is what I am looking for:
Read line of the file
store it in a temp variable
do some operation/testing
loop till the end of the file.
That's not the most efficient way to process a text file. Can you post what you want to do with the text in the file? There are plenty of tools which will operate on all of the lines in a file rather than being called line by line (e.g. sed, grep, awk, etc.).
That's not the most efficient way to process a text file. Can you post what you want to do with the text in the file? There are plenty of tools which will operate on all of the lines in a file rather than being called line by line (e.g. sed, grep, awk, etc.).
I would like to run the command cat /dev/null on the text (for example #cat /dev/null > file1.txt), since the text on each line is a file on the disk. I would really appreciate if you tell me how to read line by line through a shell script. I have tired the following command but it does not work, that's why I am asking this question
Code:
find /home/user/test -type f -exec cat /dev/null > {} \;
I have spent two entire days to get this working but I was not able to.
Thanks in advance
Last edited by kushalkoolwal; 01-26-2006 at 05:17 PM.
I have a file called test.txt which I would like to read line by line in a while loop and store the contents of every line in a variable, do some processing and again loop back to the next line until I reach at the end of the file(test.txt) through a shell script.
Basically, here is what I am looking for:
Read line of the file
store it in a temp variable
do some operation/testing
loop till the end of the file.
Thanks
You need to pipe the read command to read line by line. Something like this:
Code:
cat test.txt | while read a_line
do
echo "Now I have read the line: $a_line"
done
In every execution of the while loop the $a_line variable will contain the next line.
Last edited by perfect_circle; 01-26-2006 at 05:36 PM.
I would like to run the command cat /dev/null on the text (for example #cat /dev/null > file1.txt), since the text on each line is a file on the disk. I would really appreciate if you tell me how to read line by line through a shell script. I have tired the following command but it does not work, that's why I am asking this question
If I understand you correctly you want to empty all the
files whose name & path are stored in list.txt ...
To achieve that you could just:
I assume you are trying to create empty files with "cat /dev/null > somefile.txt". Do you realize that if a file already exists will be made empty (i.e. all content deleted) as well? If this is not what you want, or if it doesn't matter, just do:
Code:
xargs touch <test.txt
The code above will not truncate already existing files (but it will update the date/time as if they were modified).
If you do want to truncate an existing file:
Code:
while read L; do echo -n >$L; done <test.txt
This will cause an error if an empty line exists in "test.txt", also if the last line is empty. To prevent that:
Code:
while read L; do test $L && echo -n >$L; done <test.txt
I wonder why
find /home/user/test -type f -exec cat /dev/null > {} \;
and
cat list.txt|xargs -i cat /dev/null > {}
do not work although they look ok to me..
Isn;t that strange?
They are not OK. Here's why:
Code:
find /home/user/test -type f
This will will search for any file called "/home/user/test". There will be only one file found: /home/user/test.
So you could expect the the file containing the list of file ("list.txt") would be truncate to zero-length! But you were lucky this did not happen, because of another error:
When redirecting to {} , the -exec option does not replace {} with the files found by "find". So /dev/null is catted to a file called "{}". I bet you have a an empty file now in your directory called "{}".
Code:
xargs -i cat /dev/null > {}
The same problem here with redirecting to {}.
Also, "xargs" will put as many arguments from its standard input (stdin) as possible. So the command actually executed will be something like:
Bummer - it's my bad, the redirect within xargs doesn't work; the
output of xargs is being redirected to {} rather than redirecting the
output to the substituted filename. Sorry to get your hopes up :/
Alternatively create a script
#!/bin/bash
cat /dev/null > $1
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.