LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-07-2007, 11:32 AM   #1
christianunix
Member
 
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[ ]
do
touch $filename
echo $content > &filename
done

I appreciate your kindness in advance

in Christ

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

Rep: Reputation: 62
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:
Code:
#!/bin/bash

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

Matthew
 
Old 10-07-2007, 12:38 PM   #3
christianunix
Member
 
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...
 
  


Reply


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


All times are GMT -5. The time now is 09:16 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