Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 10-07-2007, 11:32 AM   #1
Registered: Oct 2007
Posts: 40

Rep: Reputation: 15
UNIX scripting question

I have two text files, and the two files contain the exact same number of lines. I would like to create a for loop that creates a group of files. each file only contain one line. the first text file contains the names of the files, and the second file contains the content of files.

I am thinking about this

for[ ]
touch $filename
echo $content > &filename

I appreciate your kindness in advance

in Christ

Old 10-07-2007, 12:09 PM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Hello, and welcome to LQ.

One stylistic hint - you can use [code] tags to preserve formatting of posted source code, or anything else which should be displayed in a fix-width font.

Ok, to your question... You can prefix a command or loop with "[n]<filename" to open the file filename and assign this open file to the numerical descriptor [n]. This file descriptor can then be passed to read and some other shell-internal commands. You can open more than one file this way.

The descriptors 0 1 and 2 are usually pre-allocated to be standard input, standard output and standard error, and you should not use numbers greater than 9 as these might interfere with the shell's own file operations. So you have 3 - 9 to play with.

Here's a very simple implementation of what you want to do:

If you have two files, names and contents, the former containing the file names you would like to use (one per line), the latter containing the data you with to put into the resulting files (one file's content per line), you can do it like this:

while read -u3 filename; do
        read -u4 data
        echo "$data" > "$filename"
done 3<names 4<contents
The read command can use the -u option to specify which file descriptor to read from. The default is standard input.

One thing to be a little careful of here: if the file contents does not have as many lines as names, you will end up having some nearly empty files. They will in fact contain a single new line character as the "" in the echo line will still print the newline character.

In humanity

Old 10-07-2007, 12:38 PM   #3
Registered: Oct 2007
Posts: 40

Original Poster
Rep: Reputation: 15
Thank you Matthew in UK! I cannot believe I received such a through help so immediately, for free. Just to let you know, I live in Toronto, Canada. Where in UK do you live? Manchester... London...


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
Linux to train team members in UNIX scripting bogsdb Linux - Software 4 10-27-2004 05:42 AM
Unix Shell Scripting, detecting the enter key WinterSt Programming 7 09-01-2004 08:27 AM
unix scripting lapthorn Programming 7 03-11-2004 09:03 AM
php and unix scripting murshed Programming 3 06-26-2003 03:56 PM
scripting the unix command script lmcthbe Linux - General 7 05-28-2003 02:49 PM > Forums > Linux Forums > Linux - Software

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

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