read line by line a text file and define a file from each file
I am writing a script file in bash. I have a text file with a list of filenames. I want to read each line (filename), open the file and get a certain value from it and print it in the original file containing the list. This is the script Im trying to use
while read line file_name; do for file in $line do line=${file%\.*} echo "Processing file $file" ctemp=`awk '{print $1;}' $file` c=`awk '{$2=$ctemp;print}' file_name` echo "$c" > file_name done done Unfortunately, nothing happens when I run it,, can anyone please help me with that,, thanks |
Firstly, please use [code][/code] tags around your code.
You may wish to have a look at what your outer loop is reading from? Currently it is standard input, ie you will need to enter the line information and file_name by hand. I am guessing this is not what you desire. Another thing to remember once you solve the above, the file stored in file_name will be open for reading until the while loop is concluded, hence writing back to it may be an issue you need to consider. |
There are two basic errors in your code.
First: Code:
read line file_name Code:
$ read line file_name There are several threads on the effects of the IFS on the read command. There's one by grail: http://www.linuxquestions.org/questi....php?p=4664136. To read a file line-by-line in a while loop, use: Code:
while read line ; do Code:
echo "$c" > file_name Code:
$ echo "Hello World" > file_name Code:
while read line ; do |
Thanks for your reply!!
Ok I made the modifications you did and this is my code now Code:
while read line Code:
awk: fatal: cannot open file `_' for reading (No such file or directory) |
Ok so I made a slight modification in the input file "file_name" and what I get now in the output file "file_name.new" is only the value for the last file,, so is there a way that I can prevent new data from overriding the previous ones,, thanks again!!
|
Okk its solved now I just did that
Code:
echo "$c" >> file_name.new |
All times are GMT -5. The time now is 06:47 PM. |