LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 05-16-2012, 11:24 AM   #1
geokker
Member
 
Registered: Jun 2004
Distribution: Ubuntu 6.10 Edgy Eft
Posts: 60

Rep: Reputation: 15
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!
 
Old 05-16-2012, 11:34 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,910

Rep: Reputation: Disabled
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.
 
Old 05-16-2012, 11:57 AM   #3
Slackyman
Member
 
Registered: Mar 2011
Location: Rome - Italy
Distribution: Slackware 13.1
Posts: 347

Rep: Reputation: 44
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).
 
Old 05-16-2012, 12:04 PM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,910

Rep: Reputation: Disabled
Quote:
Originally Posted by Slackyman View Post
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
 
Old 05-16-2012, 12:07 PM   #5
Slackyman
Member
 
Registered: Mar 2011
Location: Rome - Italy
Distribution: Slackware 13.1
Posts: 347

Rep: Reputation: 44
Obviuosly you can accomplish the same task in different ways.
That's the beautiful part of sctipting
 
Old 05-16-2012, 03:47 PM   #6
geokker
Member
 
Registered: Jun 2004
Distribution: Ubuntu 6.10 Edgy Eft
Posts: 60

Original Poster
Rep: Reputation: 15
Thumbs up

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.
 
Old 05-16-2012, 03:50 PM   #7
alinas
Member
 
Registered: Apr 2002
Location: UK, Sywell, EGBK
Distribution: RHEL, SuSE, CentOS, Debian, Ubuntu
Posts: 60

Rep: Reputation: 20
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
 
  


Reply

Tags
bash scripting, command line


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch merging pairs of files in a directory to unique file names bonissen Linux - Newbie 5 11-29-2011 01:57 PM
Need to find if a matching file exist from a list of possible file names wit_273 Linux - General 5 10-25-2007 09:47 AM
copying files and give new unique names to each file by using xargs command gnim66 Programming 6 06-22-2005 08:29 PM
create unique user names sabapathys Linux - Software 1 05-12-2005 05:50 AM
create unique user names sabapathys Linux - Newbie 1 05-12-2005 04:09 AM


All times are GMT -5. The time now is 01:28 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration