Create populated files with unique file names from a list
I want to create populated files with unique file names from a list.
list_of_filenames.csv: orange apple banana coconut .. A script will produce files: orange.txt apple.txt banana.txt coconut.txt .. Each file will have a similar template inside: e.g. orange.txt: This is the text inside orange. it is very nice text that I like a lot. If you want an orange please buy one. e.g. apple.txt: This is the text inside apple. it is very nice text that I like a lot. If you want an apple please buy one. What is the simplest, efficient way of doing this? The list could be 100,000 entries long! |
Code:
cat list_of_filenames.csv | while read x ; do ( echo Template text for $x > $x.txt ) ; done |
You anticipated me, Ser Olmy! :)
But I think having a template in a file can be convenient: Code:
cat list_of_filenames.csv | while read x ; do ( cat template.txt > $x.txt ) ; done |
Quote:
Code:
cat list_of_filenames.csv | while read x ; do ( cat template.txt | sed "s/_placeholder_/$x/g" > $x.txt ) ; done |
Obviuosly you can accomplish the same task in different ways.
That's the beautiful part of sctipting :) |
Thanks guys, you rock!
This is what I went with: Code:
#!/bin/bash |
The following is a bit more complex, but more efficient, as most of the work is done by the shell itself, without a need for external commands. The <<< is a 'here string'. If you need more lines than one in your template, you could replace it with a << syntax, the 'here document' method.
Code:
#!/bin/bash |
All times are GMT -5. The time now is 09:58 PM. |