Create files from a list and then populate them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create files from a list and then populate them
# 1  
Old 06-19-2011
Create files from a list and then populate them

I have a list of filenames that I want created - they must be created via a certain naming convention due to the software I'm using. Once they are created I have another file that will be used to populate them with data. So far this is what I have:
Code:
#For each line in text file "foo_txt" create a file with that name:

for i in $(cat create/foo_txt); do
        touch create/foodir/$i;

#For each line in text file "bar_txt" store the line of text in each file created by foo_txt:

for i in $(cat create/bar_txt); do echo $i >> create/foodir/<each_file_from_foo_txt>; done

For example, foo_txt would contain the following:
Code:
file_a
file_b
file_c
file_d

And four files would be created. bar_txt would contain the following:
Code:
 new_id1, file_a, 1
new_id2, file_b, 2
new_id3, file_c, 3
new_id4, file_a, 4
new_id5, file_a, 5
new_id6, file_b, 6

Some of the lines from bar_txt would be appended to a file created from foo_txt which is why I'm using the ">>", and that's why the files from foo_txt would contain more than 1 line from bar_txt - whatever filename is listed in foo_txt at the time each line of bar_txt is read.SmilieSmilieSmilie
# 2  
Old 06-19-2011
Your description was somewhat confusing to me. If I understand what you want, this may get you started:

Code:
#!/usr/bin/env ksh
while read file junk
do
    touch $file
done <foo

while read junk1 file junk2
do
    if [[ -f ${file%%,*} ]]                # only if previously created
    then
        echo "$junk1 $file $junk2" >>${file%%,*}
    fi
done <bar

I avoid using constructs like for x in $(cat some-file) do because the number of lines in the file might exceed the system parm limits, and/or there might be multiple words on the file where only one of which is the desired filename. The using while read does the same thing without these dangers.

I made the assumption that lines from 'bar' were only to be placed into a newly created file if the filename existed, and the line was to be written only to the named file. Thus filea would only get the lines from bar that had filea listed as the second token.

You should be able to augment this example with proper directories etc. and please be advised that I did NOT test this code.

Last edited by agama; 06-19-2011 at 02:48 PM.. Reason: typo
# 3  
Old 06-19-2011
It is confusing and I appreciate your help and decrypting my request. Let me just add a little more explanation. When I originally referred to the "bar_txt" file - that's a file containing a list of /etc/passwd entries. The "foo_txt" file contains the name of the server where the /etc/passwd entry will be created. So, I usually have a "foo_txt" file that has repeating entries depending on how many lines of the bar_txt file will be input to the respective file created from foo_txt. I tried to denote this as the server name is listed in each line of the bar_txt file - it would be where you see "file_a" or "file_b" etc. I probably could have written the question a little better.

In any event I appreciate your reply and I'll give it a chance when I can - just thought I'd throw up a quick post and see if I could get some guidance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a list of files from alias by script

Actually I have many pictures with diferent name and size around 2000, I need generate a copy of them from one list of alias. The structure of the list is something like this: alias_list.txt <01>randomname.png<02> Randomname.png RandoMname.png RandOmname.png RandomnamE.png... (6 Replies)
Discussion started by: Tapiocapioca
6 Replies

2. Shell Programming and Scripting

Scan directories and create a list of files

Gents, Please can you help. I want to create a list which contends the complete patch of the location of some directories with the size of each file. need to select only .txt file In this case I am try to find the subdirectories tp1 and tp2 and create the output list. jd175-1 tp1... (3 Replies)
Discussion started by: jiam912
3 Replies

3. Shell Programming and Scripting

Create a list of missing files

Hello Guys I have a big list of files in one directory. And Some are missing . Example ls -l 2191off-r0.sps 2193off-r0.sps 2194off-r0.sps 2197off-r0.sps 2198off-r0.sps 2200off-r0.sps I would like to create a file with the list only missing files. Or if is possible to create in ... (4 Replies)
Discussion started by: jiam912
4 Replies

4. Shell Programming and Scripting

Create empty files from a list on file

Hello Guys. Please I would like to create empty files from a list In file1 will be the followin values, so i will like to create for each name a empty file. file1 2191off-r0.sps 2192off-r0.sps 2193off-r0.sps 2194off-r0.sps 2195off-r0.sps So I need to get 5 empty files. Thanks for... (7 Replies)
Discussion started by: jiam912
7 Replies

5. Solaris

Command to list all header files used to create an executable

All, I would like to know if there is a command which will list all the header files which were used while creating the executable. (1 Reply)
Discussion started by: helper
1 Replies

6. UNIX for Dummies Questions & Answers

Need help how to create a file (xml) list all files from directory

I have more than 10K songs in two directories on a hard drive. I would like to create a file list all of files name then change to .xml extension to upload to iPhone so I have a Karaoke list on my iPhone. I need your help to create a file by using command in Linux. Files names: 0001 More... (4 Replies)
Discussion started by: ggcc
4 Replies

7. Shell Programming and Scripting

How to create multiple list of files in a directory ?

Hi, i have say 100 files in a directory. file1.log file2.log file3.log file4.log file5.log file6.log ... ... ... file99.log file100.log ========= I need to create another file which contains the list of al these log files. each file should contain only 10 log file names. it shud... (4 Replies)
Discussion started by: robinbannis
4 Replies

8. Shell Programming and Scripting

create diffrent files based on other file and parameters list

I would like ot create shell script/ bash to create diffrent files based on a file and parameters list. Here is the detail example: I have a textfile and four static parameter files (having ‘?'). mainfile.txt has below records (this count may be more than 50) A200001 A200101 B200001... (9 Replies)
Discussion started by: raghav525
9 Replies

9. Shell Programming and Scripting

Shell Script Create List of Deleted Files

Hi, I want to put all the deleted files in a txt file. Because i want to backup my image server which has thousands of jpg images. I wrote a shell script which will copies images from image server to backup image server. I setup a cronjob which runs on every five minutes. & through timestamp it... (8 Replies)
Discussion started by: mirfan
8 Replies

10. UNIX for Dummies Questions & Answers

Create a list of files that were modified after a given date.

Hello Mates! I'm kinda new to unix and need to a solve a problem. Input: date Situation: With the given date I need to find a list of all such files starting from a given path that were modified after the given date. I experimented with the "find" with "-newer" but did not quite get it... (4 Replies)
Discussion started by: rkka
4 Replies
Login or Register to Ask a Question