LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-09-2012, 04:22 PM   #1
yasmine
LQ Newbie
 
Registered: Dec 2012
Posts: 4

Rep: Reputation: Disabled
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
 
Old 12-09-2012, 07:04 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,471

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
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.
 
Old 12-09-2012, 10:34 PM   #3
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 118Reputation: 118
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.

Last edited by towheedm; 12-09-2012 at 10:36 PM.
 
Old 12-10-2012, 04:24 AM   #4
yasmine
LQ Newbie
 
Registered: Dec 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Unhappy

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
 
Old 12-10-2012, 04:50 AM   #5
yasmine
LQ Newbie
 
Registered: Dec 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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!!
 
Old 12-10-2012, 04:53 AM   #6
yasmine
LQ Newbie
 
Registered: Dec 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Okk its solved now I just did that
Code:
echo "$c" >> file_name.new
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] open two text files , read them line by line and update parameters of the 3rd file rastin_nz Programming 17 10-20-2010 07:10 PM
open text file and read line by line amani_87 Linux - Software 20 01-28-2010 10:25 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM


All times are GMT -5. The time now is 11:41 AM.

Main Menu
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