21

FILE:

hello
world

I would like to use a scripting language (BASH) to execute a command that reads each WORD in the FILE above and then plugs it into a command.

It then loops to the next word in the list (each word on new line). It stops when it reaches the end of the FILE.


Progression would be similar to this:

Read first WORD from FILE above

Plug word into command

command WORD > WORD
  • which will output it to a text file; with word as the name of the file.

Repeat this process, but with next to nth WORD (each on a new line).

Terminate process upon reaching the end of FILE above.


Result of BASH command on FILE above:

hello:

RESULT OF COMMAND UPON WORD hello

world:

RESULT OF COMMAND UPON WORD world
user191960
  • 1,941
  • 5
  • 20
  • 24
  • No - its a hobby-project to create a list of all French Verb conjugations from a large list of French verbs. – user191960 Oct 22 '09 at 05:55
  • 800 French Verbs -> french-conjugator (debian) -> Create a directory of conjugations -> Make Flash Cards -> I'll publish the link when I'm done (quizet.com). – user191960 Oct 22 '09 at 05:59
  • 5
    @ghostdog74 - How could you doubt me? You've been helping me for a couple of days ;) with text manipulation under *nix systems. I guess the question does look a bit homeworky - but I made it so that others may benefit via my abstraction. Else I could've just plugged in what I wanted. – user191960 Oct 22 '09 at 06:05

3 Answers3

35

You can use the "for" loop to do this. something like..

for WORD in `cat FILE`
do
   echo $WORD
   command $WORD > $WORD
done
Rahul
  • 12,886
  • 13
  • 57
  • 62
  • Worked perfectly! As did those below - but included files with odd characters (apostrophes). Should've put that in the question - but I tried to abstract it as much as possible. – user191960 Oct 22 '09 at 06:18
20

normally i would ask what have you tried.

while read -r line 
do 
   command ${line} > ${line}.txt
done< "file"
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
8
IFS=$'\n';for line in `cat FILEPATH`; do command ${line} > ${line}; done
Vissu
  • 328
  • 1
  • 2
  • 8