LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Create populated files with unique file names from a list (https://www.linuxquestions.org/questions/linux-server-73/create-populated-files-with-unique-file-names-from-a-list-945325/)

geokker 05-16-2012 11:24 AM

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!

Ser Olmy 05-16-2012 11:34 AM

Code:

cat list_of_filenames.csv | while read x ; do ( echo Template text for $x > $x.txt ) ; done
If you use this in a script, it can easily be extended to include a multi-line template text using, say, a here document.

Slackyman 05-16-2012 11:57 AM

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
So that the template.txt can contain the template text (also multiline).

Ser Olmy 05-16-2012 12:04 PM

Quote:

Originally Posted by Slackyman (Post 4680094)
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
So that the template.txt can contain the template text (also multiline).

Certainly. In the example, the template text contained a reference to the name from the csv file. If you don't use a here document (which could contain a variable), the template should probably be fed through sed.

Code:

cat list_of_filenames.csv | while read x ; do ( cat template.txt | sed "s/_placeholder_/$x/g" >  $x.txt ) ; done

Slackyman 05-16-2012 12:07 PM

Obviuosly you can accomplish the same task in different ways.
That's the beautiful part of sctipting :)

geokker 05-16-2012 03:47 PM

Thanks guys, you rock!


This is what I went with:


Code:

#!/bin/bash

template=$(cat <<'template_text'


A bunch of
template text
goes here


template_text
)

cat list | while read entries ;
do ( echo $template $entries > $entries.txt ) ;
done

where 'list' is a file containing words on separate lines.

alinas 05-16-2012 03:50 PM

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
while read file
do
  > ${file}.txt <<<"My template string"
done < files.cvs



All times are GMT -5. The time now is 09:58 PM.