I have a file, foo.txt
, that has a folder name on each line:
folder_1
folder_2
folder_3
And I'd like to do something like this:
cat foo.txt | xargs -I {} echo 'function {}() { return stuff; }' > {}/function.js
In other words, I'd like to read a file line by line, then use each line to both create a string and create the name of the file in which the string is stored.
How does one do something like this from the command line in bash?
Best Answer
Just loop over all lines in the file:
Of course this assumes you have already directories named as lines in the
foo.txt
. If this is not the case then first create them withmkdir "$line"
.Another approach, with
awk
instead of loop would be: