Efficient Text File Writing


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Efficient Text File Writing
# 1  
Old 04-26-2012
Java Efficient Text File Writing

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Write a template main.c file via shell script to make it easier for yourself later.
The issue here isn't writing the file though, I can do that with echo statements.
What I'd like to see is how to do this in a less redundant way? Note down in the attempts at solution, the amount of echos and >> main$ext is completely unnecessary.
So I'd like to see an alternative, more "efficient for the programmer", way of doing it.
Is there any way to work with a block of text within the shell script?

Edit: I'd also like to avoid all one echo statement with a bunch of "\n" characters, that would ALSO be redundant in my opinion.

2. Relevant commands, code, scripts, algorithms:
ext=".c"
proj="proj"
the echo statement
and >> the append redirection operator

3. The attempts at a solution (include all code and scripts):
Code:
#! /bin/bash
#  main.c
ext=".c"
read proj
echo "//$auth" > main$ext
echo "//main.$ext" >> main$ext
echo "//$proj main file" >> main$ext
echo "" >> main$ext
echo "#include <iostream>" >> main$ext
echo "#include <cstdlib>" >> main$ext
echo "" >> main$ext
echo "#include \".h\"" >> main$ext
echo "" >> main$ext
echo "" >> main$ext

The finished product looks akin to
Code:
//$auth
//main.c
//$proj main file/implementation

#include <iostream>
#include <cstdlib>

#include ".h"

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
University of California Davis, Davis, CA, USA, Sean Davis, ECS40
(This is extra work outside of class though so it really has nothing to do with school. I just like shell scripting lately)

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by Scrutinizer; 04-26-2012 at 05:31 PM..
# 2  
Old 04-26-2012
Hi this could be done with a so-called "here document":

Code:
cat > file << EOF
//$auth
...
EOF

See man bash
# 3  
Old 04-27-2012
Scrutinizer is correct, but as there are always several ways in Unix to do something, you could do it with the exec too, if you use ksh (note that this won't work in bash, because bash doesn't have the print command):

Code:
exec 3> /some/output/file   # open the file as I/O channel 3 and clear
                            # its contents if it already exists

print -u3 "some text"       # print some text to channel 3
print -u3 "something else"  # same
...

exec 3>&-                   # close I/O-channel 3

This way you won't have to take care of the first ">" and subsequent ">>" redirections (if you mistakenly use ">" it will clear the file) and you will not have to repeat the output files name over and over again.

You could even influence the content of your output file with some program logic in the script - something the here-document provides little support for. For instance, the following would not be possible with a here-doc:

Code:
exec 3> /some/output/file

print -u3 "first line"
print -u3 "second line"

if [ <some_criteria> ] ; then
     print -u3 "----Optional line------"
fi
print -u3 "third line"

exec 3>&-


In exchange for helping you I'd like to ask you to learn about I/O channels (file descriptors) and redirection in Unix and how to use these. You might want to read this thread as a starter.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Portable and efficient way to add text after pattern

Shell: sh/bash OS: Linux (all unix flavors) Suppose i have a variable with this content: ArgZ=' import os import sys MySpecialpath = os.path.abspath(sys.argv) # ' ArgZB='#REGEN #REGEN #REGEN ' I want to add this text to a file/script, only under the following conditions: 1. ... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Efficient way to search array in text file by awk

I have one array SPLNO with approx 10k numbers.Now i want to search the subscriber number from MDN.TXT file (containing approx 1.5 lac record)from the array.if subscriber number found in array it will perform below operation.my issue is that it's taking more time because for one number it's search... (6 Replies)
Discussion started by: siramitsharma
6 Replies

3. Shell Programming and Scripting

Most efficient method to extract values from text files

I have a list of files defined in a single file , one on each line.(No.of files may wary each time) eg. content of ETL_LOOKUP.dat /data/project/randomname /data/project/ramname /data/project/raname /data/project/radomname /data/project/raame /data/project/andomname size of these... (5 Replies)
Discussion started by: h0x0r21
5 Replies

4. Shell Programming and Scripting

Efficient population of array from text file

Hi, I am trying to populate an array with data from a text file. I have a working method using awk but it is too slow and inefficent. See below. The text file has 70,000 lines. As awk is a line editor it reads each line of the file until it gets to the required line and then processes it.... (3 Replies)
Discussion started by: carlr
3 Replies

5. UNIX for Dummies Questions & Answers

Efficient way of extracting data from file

I am having a file, around 500 lines. which contains one letter words, two letters words,...and so on(up to 15 letter words and words are not seprated by line). I need to compare all 1 letter words with 3,4,5 and 6 letters word, all 2 letters words with 2,3,4 and 5 letters words and all 3 letters... (3 Replies)
Discussion started by: akhay_ms
3 Replies

6. UNIX for Dummies Questions & Answers

Writing text to file

Hi, I know the code to write a piece of text to the end of a given text file is echo $text >> filename.txt I would like to know how to write a piece of text to a file using shell, but the file name isn't given. I want it to write to whatever text file is currently open. Not to all text files... (2 Replies)
Discussion started by: anirudh215
2 Replies

7. Shell Programming and Scripting

Need help in writing a script to create a new text file with specific data from existing two files

Hi, I have two text files. Need to create a third text file extracting specific data from first two existing files.. Text File 1: Format contains: SQL*Loader: Release 10.2.0.1.0 - Production on Wed Aug 4 21:06:34 2010 some text ............so on...and somwhere text like: Record 1:... (1 Reply)
Discussion started by: shashi143ibm
1 Replies

8. UNIX for Dummies Questions & Answers

efficient raid file server

I need to put together a RAID1 file server for use by Windoze systems. I've built zillions of windows systems from components. I was a HPUX SE for a long time at HP, but have been out of the game for years. I've got an old workhorse mobo FIC PA-2013 with a 450 MHz K6 III+ I could use, but I'd... (2 Replies)
Discussion started by: pcmacd
2 Replies

9. Shell Programming and Scripting

writing data in a text file at particular line

I need to write value of variable $version at a particular line in a text file. Line number is determined by another variable &line......I don't know how to do it in shell script ... (2 Replies)
Discussion started by: punitpa
2 Replies

10. UNIX for Dummies Questions & Answers

Picking out text from one file and writing it to another.

Hello. I have one file that is a collection of discarded emails. Each email is it's own section with each section beginning with the same header (ie 'Another Email' ). I want to traverse through the file and every time I find the header ('Another Email') I then want to pick out the 'To:' line... (5 Replies)
Discussion started by: scottf33
5 Replies
Login or Register to Ask a Question