LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   read line by line a text file and define a file from each file (https://www.linuxquestions.org/questions/linux-newbie-8/read-line-by-line-a-text-file-and-define-a-file-from-each-file-4175440707/)

yasmine 12-09-2012 04:22 PM

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

grail 12-09-2012 07:04 PM

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.

towheedm 12-09-2012 10:34 PM

There are two basic errors in your code.

First:
Code:

read line file_name
Let's assume we do this from the CLI:
Code:

$ read line file_name
Hello World
$ echo $line
Hello
$ echo $file_name
World

$ read line file_name
This is a test
$ echo $line
This
$ echo $file_name
is a test

The read command will assign the first field to the variable line and the all other fields (separated by the IFS) to the variable 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
  echo "$line"
done < file_name

Second:
Code:

echo "$c" > file_name
will overwrite the contents of file_name with the contents of the variable c. To append to the contents of a file using redirection, use >>.

Code:

$ echo "Hello World" > file_name
$ cat file_name
Hello World

$ echo "Hello Earthlings" > file_name
$ cat file_name
Hello Earthlings

$ echo "From your Martian neighbours" >> file_name
$ cat file_name
Hello Earthlings
From your Martian neighbours

Of course, with the while loop, while you cannot read and write to the same file at the same time (as grail mentioned), you can redirect the while loop to also write to a new file. You can then copy the contents of the new file to file_name:
Code:

while read line ; do
  echo "$line add this"
done < file_name > file_name.new
mv file_name.new file_name

Hope this helps.

yasmine 12-10-2012 04:24 AM

Thanks for your reply!!
Ok I made the modifications you did and this is my code now

Code:

while read line
do
#filename=`awk '$line' "file_name"

for file in $line
do
line=${file%\.*}
echo "Processing file $file"
wait $!
ctemp=`awk '$1' "$file"`
c=`awk '{$1 =$ctemp;print}' file_name.new`
echo "$c" > file_name.new
done
done < file_name]

what I get is Processing file $file "it states all the filenames listed",then
Code:

awk: fatal: cannot open file `_' for reading (No such file or directory)
so I guess the proble now is in the for loop

yasmine 12-10-2012 04:50 AM

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!!

yasmine 12-10-2012 04:53 AM

Okk its solved now I just did that
Code:

echo "$c" >> file_name.new
:)


All times are GMT -5. The time now is 12:22 AM.