Script to read line by line from a file
Hi guys,
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 |
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.).
|
Quote:
Code:
find /home/user/test -type f -exec cat /dev/null > {} \; Thanks in advance |
Quote:
Code:
cat test.txt | while read a_line |
Quote:
files whose name & path are stored in list.txt ... To achieve that you could just: cat list.txt|xargs -i cat /dev/null > {} Cheers, Tink |
Quote:
Any ideas why? Kushal |
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 If you do want to truncate an existing file: Code:
while read L; do echo -n >$L; done <test.txt Code:
while read L; do test $L && echo -n >$L; done <test.txt |
Quote:
Hey Perfect Circle, thanks a lot. You made my day. You are really perfect. The suggestion you gave worked perfectly for me. Thanks a lot dude. 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? |
Not without more input :}
What does the file look like internally, are the paths to the files you want to empty absolute? Cheers, Tink |
Quote:
Code:
./file1.txt |
Quote:
Code:
find /home/user/test -type f 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 > {} Also, "xargs" will put as many arguments from its standard input (stdin) as possible. So the command actually executed will be something like: Code:
cat /dev/null > file1 file2 file3 file4 |
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 and feed the cat to that ... :} cat list.txt|xargs -i empty {} works ... Cheers, Tink |
Quote:
|
Quote:
Quote:
Cheers, Tink |
Quote:
I missed that part. I should have read your post less quickly. |
Quote:
Truly said "When you work with linux, everyday you learn something new"...:D |
Quote:
Quote:
Cheers, Tink |
Quote:
Hko, you are right I do see file '{}' in my directory. But, then why does the command Code:
find ./ -type f -exec less {} \; ??? |
Quote:
So you could do: Code:
find ./ -type f -exec cat {} > /dev/null \; Code:
find ./ -type f -exec cat /dev/null > {} \; |
I don't think it matters where the redirect is in the command. It's being applied for the 'find' not the command in the exec statement.
The easiest way around this is to dump the exec stuff into a sub-shell. For example: Code:
find . -type f -exec sh -c 'cat /dev/null > {}' \; |
Quote:
Thanks for clearing that up. |
All times are GMT -5. The time now is 02:21 PM. |