openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting » Processing A Txt File One Line At A Time

Go Back   openSUSE Forums > Archives > SF Archives > ARCHIVES - Programming & Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


ARCHIVES - Programming & Scripting A place to discuss website design, programming, shell scripts, etc

 
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 29-Feb-2008, 11:54
Arcath
Guest
 
Posts: n/a
Default

I have written a program the uses

Code:
ls -R >/tmp/backup/list.txt
to generate a list of files.

It puts one file per line, i need to be able to go through the file one line at a time and run another program on that file.

so basically i need some kind of for...next loop for each line of the file
  #2 (permalink)  
Old 29-Feb-2008, 15:10
Arcath
Guest
 
Posts: n/a
Default

ive solved this problem

Code:
cat $file | while read line; do
...
done
$file is the list, and ... is the code to be run fr each line
  #3 (permalink)  
Old 06-Mar-2008, 22:23
suseforumsnet42
Guest
 
Posts: n/a
Default

Quote:
so basically i need some kind of for...next loop for each line of the file
[/b]
Note, piping can get you in trouble, because the loop will be done in a subshell. You'll be scatching your head why variables after the loop don't reflect changes made within the loop ;-)

Some options:

while read line; do echo $line; done < your_file

while read line; do echo $line; done < <(cat your_file) # No space between <(
while read line; do echo $line; done < <(issue your_command here to generate list of files)

for line in "$(cat your_file)"; do echo "$line"; done
for line in "$(< your_file)"; do echo "$line"; done

Hope that helps with some of your options.
  #4 (permalink)  
Old 06-Mar-2008, 22:28
ken_yap
Guest
 
Posts: n/a
Default

You should also be careful to quote any use of $line inside the loop because if you have a filename with spaces, the command will get more than one argument.

while read line
do
command "$file"
done < your_file
 

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2