Sponsored Content
Full Discussion: Efficient Text File Writing
Homework and Emergencies Homework & Coursework Questions Efficient Text File Writing Post 302631023 by george3isme on Thursday 26th of April 2012 03:59:12 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ECHO(3) 								 1								   ECHO(3)

echo - Output one or more strings

SYNOPSIS
void echo (string $arg1, [string $...]) DESCRIPTION
Outputs all parameters. echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses. echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syn- tax only works with the short_open_tag configuration setting enabled. I have <?=$foo?> foo. PARAMETERS
o $arg1 - The parameter to output. o $... - RETURN VALUES
No value is returned. EXAMPLES
Example #1 echo examples <?php echo "Hello World"; echo "This spans multiple lines. The newlines will be output as well"; echo "This spans multiple lines. The newlines will be output as well."; echo "Escaping characters is done "Like this"."; // You can use variables inside of an echo statement $foo = "foobar"; $bar = "barbaz"; echo "foo is $foo"; // foo is foobar // You can also use arrays $baz = array("value" => "foo"); echo "this is {$baz['value']} !"; // this is foo ! // Using single quotes will print the variable name, not the value echo 'foo is $foo'; // foo is $foo // If you are not using any other characters, you can just echo variables echo $foo; // foobar echo $foo,$bar; // foobarbarbaz // Some people prefer passing multiple parameters to echo over concatenation. echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10); echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . " "; echo <<<END This uses the "here document" syntax to output multiple lines with $variable interpolation. Note that the here document terminator must appear on a line with just a semicolon. no extra whitespace! END; // Because echo does not behave like a function, the following code is invalid. ($some_var) ? echo 'true' : echo 'false'; // However, the following examples will work: ($some_var) ? print 'true' : print 'false'; // print is also a construct, but // it behaves like a function, so // it may be used in this context. echo $some_var ? 'true': 'false'; // changing the statement around ?> NOTES
Note Because this is a language construct and not a function, it cannot be called using variable functions. SEE ALSO
print(3), printf(3), flush(3), Heredoc syntax. PHP Documentation Group ECHO(3)
All times are GMT -4. The time now is 06:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy