LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 08-20-2009, 11:06 AM   #1
astroumut
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Rep: Reputation: 0
How can I read a file line by line and add it to a loop in another file?


Hi All,

I have a problem for making a for or a while loop to the following example.

I have two files

------------------------
filelist.dat, which contains
-----------------------
dummy1.dat
dummy2.dat
dummy3.dat
...
------------------------------
and
------------------------------
conversion.sh which contains (it is longer but nevermind),
------------------------------
set listcube = dummy1.dat
@ i = 0
while ($i <= 3)
@ j = 0
while ($j <= 3)
echo "Finished ${listcube}_${i}_${j}.dat"
@ j ++
end
@ i ++
end
------------------------------------


My question is, I have a long list of files like dummyx.dat and I just want read one line from filelist.dat and put this line to the the first line of conversion.sh file like
set listcube = dummy1.dat,
then the rest is the same in the conversion.sh
set listcube = dummy2.dat
then the rest is the same in the conversion.sh

How can I make a loop for that.
I hope it is clear.

Thanks for any help.

Umut
 
Old 08-20-2009, 12:46 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Which language are you writing in? Your file name ends in .sh but it doesn't look like shell script.

It would be easier to read your code if you put CODE tags around it. That's CDODE and /CODE enclosed in [] brackets. If you go into Advanced edit mode, you can use the # icon to automate code tags.
 
Old 08-20-2009, 03:22 PM   #3
astroumut
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Hi,
I actually run it from the command window with only ./conversion.sh . So it should be either bash or shell script.

OK, I am writing it how you wanted.



filelist.dat
Code:
dummyfile1.dat
dummyfile2.dat
dummyfile3.dat
...

conversion.sh
Code:
set listcube = dummyfile1.dat
@ i = 0
while ($i <= 3)
@ j = 0
while ($j <= 3)
echo "Finished ${listcube}_${i}_${j}.dat"
@ j ++
end
@ i ++
end

The filenames in the filelist.dat file should be read one by one in the conversion.sh file and put in a loop in it.

In the end, it should do the task like that. But with a loop it is much easier of course.

Code:
set listcube = dummyfile1.dat
@ i = 0
while ($i <= 3)
@ j = 0
while ($j <= 3)
echo "Finished ${listcube}_${i}_${j}.dat"
@ j ++
end
@ i ++

set listcube = dummyfile2.dat
@ i = 0
while ($i <= 3)
@ j = 0
while ($j <= 3)
echo "Finished ${listcube}_${i}_${j}.dat"
@ j ++
end
@ i ++

....
end


But I could not do the loop.
Please help.

Thanks
 
Old 08-20-2009, 03:31 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Since you are reading the filelist.dat file line by line:
while read filename; do
# do stuff. The variable $filename is used.
done < filelist.dat

You can also use "$(wc -l)" to return the number of lines in the file if you insist on a for loop.
 
Old 08-20-2009, 06:18 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
What are the '@' at the start of the lines?
 
Old 08-21-2009, 06:14 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by astroumut View Post
conversion.sh
Code:
set listcube = dummyfile1.dat
@ i = 0
while ($i <= 3)
@ j = 0
while ($j <= 3)
echo "Finished ${listcube}_${i}_${j}.dat"
@ j ++
end
@ i ++
end
Ignoring the "@"s pending your answer to chrism01's question ...

Code:
set listcube = dummyfile1.dat
That sets $1 to listcube, $2 to = and $3 to dummyfile1.dat. I guess you wanted to set $listcube to dummyfile1.dat in which case you can use
Code:
listcube=dummyfile1.dat
It is important that there are no spaces either side of the "=".
Code:
while ($i <= 3)
has the effect of running $i <= 3 in a subshell and testing the exit status from the subshell which is always going to be 1 (error, false). In the subshell, bash first processes the redirection operator < and finds it is being asked to redirect input from a file called =. Probably there is no such file and this will result in an error. I guess you want to loop while $i is less than or equal to 3. This will do it
Code:
while [[ "$i" -le 3 ]]
do
    <commands>
done
The double quotes around $i are defensive, in case it is empty, unset or contains only whitespace. The "-le" is an arithmetic comparison operator ("<=" is a string comparison operator). The "do" and "done" define the loop body and are essential after while to complete the "compound command".

It seems you are trying to write shellscript by guesswork rather than studying how to do it and that aint never going to work; shellscript is not intuitive. Better you netsearch for bash shellscript introductions (there are lots out there) and study some examples. The Advanced Bash Scripting guide has lots of examples.
 
Old 08-24-2009, 03:44 AM   #7
astroumut
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks a lot for the answers. I solved my problem and realized my mistake too.

In the beginning of the conversion.sh file there is

Code:
#!/bin/csh -f
so that is indeed a shell script, and the commands that you were written did not work. But when I changed it to

Code:
#!/bin/bash
then your scripts work but mine did not work. So I tried to find a middle way between. I have no idea what the @'s do, because I took it from the standard conversion file, but it looks like it has a meaning in shell.

Thanks for your answers
 
Old 08-24-2009, 04:37 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The @ in csh/tcsh is a kind of set statement to assign the value of an arithmetic expression to a variable. It let you do C-style increment and decrement, as in
Code:
@ var++
@ var--
Regarding your question, reading a file line-by-line in csh is one of the top 10 reasons to not use C shell programming. Take in mind that in csh you don't have a read statement and you can read only from the standard input. If for some reason the standard input changes during the execution of the script, the content of the file is not parsed anymore. In other words you cannot read from standard input as "terminal input" and as "file input" simultaneously.

In any case, here is a version of your script that should work:
Code:
#!/bin/csh -f
set listcube = $<
set i = 0
while ( $listcube != "" )
  @ i++
  echo line number $i is: $listcube
  set listcube = $<
end
the $< notation means "the next line of input": this is the way to read standard input line by line in csh. As you can notice there is no way to redirect the standard input from a file, unless you explicitly run the script using input redirection:
Code:
./conversion.sh < filelist.dat
 
  


Reply



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
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 12:06 PM
I would like need a suggestion on bash shell : Read a file line by line and do stuff madi3d8 Linux - Newbie 1 01-15-2009 09:30 AM
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
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:36 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration