Adding text to the top of a file without using temporary files

Adding text to the top of a file without using temporary files

Post by Andrew Sco » Sat, 20 Aug 1994 03:46:48



I want to send a text file via "mail". Before I pass the file to mail, I want
to insert at the top of the file a subject line of "Subject: < report name>".

I would dearly like to do this without using temporary files, etc. but I can't
find a way of doing it. I've tried echoing text through pipes, cating the
mail file various ways to try and prefix it with the subject line. I've looked
at sed but, not being a sed expert, I haven't found a solution that way either.

Before anyone asks, there isn't a switch to pass the subject to the mail
command. 8-(

Does anyone know of a one line solution to do this without the creation of
temporary files?

Thanks
Andrew Scott.

=====

Development Centre, IS.        
AT&T Global Information Solutions,
Kingsway West, Dundee, Scotland  DD2 3XX                
Tel: +44 382 592531  Fax: +44 382 622198

 
 
 

Adding text to the top of a file without using temporary files

Post by bbunt.. » Sat, 20 Aug 1994 02:31:27


I tried
$ (print Subject: testing ; cat .vedrc) | mail user
which worked on hpux 9.03

 
 
 

Adding text to the top of a file without using temporary files

Post by Mike Dwy » Sat, 20 Aug 1994 04:11:21




>I want to send a text file via "mail". Before I pass the file to mail, I want
>to insert at the top of the file a subject line of "Subject: < report name>".

(stuff dleted)
>Before anyone asks, there isn't a switch to pass the subject to the mail
>command. 8-(


Try this:


The output of the subshell in the parens will be passed to your mailer.  

[This also works for printing a file and its name together:

        (echo "filename\n"; cat filename) | lp
]

miked

 
 
 

Adding text to the top of a file without using temporary files

Post by Alex Ugolini 4-6436 MR » Sat, 20 Aug 1994 06:55:53



Quote:>I want to send a text file via "mail". Before I pass the file to mail, I want
>to insert at the top of the file a subject line of "Subject: < report name>".

>I would dearly like to do this without using temporary files, etc. but I can't
>find a way of doing it. I've tried echoing text through pipes, cating the
>mail file various ways to try and prefix it with the subject line. I've looked
>at sed but, not being a sed expert, I haven't found a solution that way either.

>Before anyone asks, there isn't a switch to pass the subject to the mail
>command. 8-(

>Does anyone know of a one line solution to do this without the creation of
>temporary files?

Perhaps this will work for you?

        (echo "Subject: <report name>"; cat reportfile) | mail $userids

------------------------------------------------------------------------------
Alex P. Ugolini, Jr.      General Electric Medical Systems, Magnetic Resonance

 
 
 

Adding text to the top of a file without using temporary files

Post by Juergen Pla » Sat, 20 Aug 1994 19:47:03



Quote:> I want to send a text file via "mail". Before I pass the file to mail, I want
> to insert at the top of the file a subject line of "Subject: < report name>".
> I would dearly like to do this without using temporary files, etc. but I can't
> .....

What's about:   mailx -s 'Report Name' <somefile

In general, adding a line before a file can be done by:

(echo "fooooo" ; cat file) > somewhere
(echo "fooooo" ; cat file) | program

--
    _                                                                _
  _(`>                                                              <')_

 
 
 

Adding text to the top of a file without using temporary files

Post by Andrew Dunst » Mon, 22 Aug 1994 03:58:51



|> I want to send a text file via "mail". Before I pass the file to mail, I want
|> to insert at the top of the file a subject line of "Subject: < report name>".
|>
|> I would dearly like to do this without using temporary files, etc. but I can't
|> find a way of doing it. I've tried echoing text through pipes, cating the
|> mail file various ways to try and prefix it with the subject line. I've looked
|> at sed but, not being a sed expert, I haven't found a solution that way either.
|>
|> Before anyone asks, there isn't a switch to pass the subject to the mail
|> command. 8-(
|>
|> Does anyone know of a one line solution to do this without the creation of
|> temporary files?
|>

echo "Subject: <report name>" | cat - mailfile | mail recipient

works for me!

cheers

andrew

 
 
 

Adding text to the top of a file without using temporary files

Post by Bill Morr » Wed, 24 Aug 1994 00:37:02


: In general, adding a line before a file can be done by:

: (echo "fooooo" ; cat file) > somewhere
: (echo "fooooo" ; cat file) | program

Does that work with adding something before an incoming pipe?
I want to insert a file before something that is getting piped
to lpr.

So that I get

my_inserted_file, stdin, more_files

sent to the print queue in that order, as one job. The files have to
remain separate entities, my inserted file is Postscript, and the
stdin could be anything, usually plain text.

You could redirect the stdin into some temporary file, but that is
inefficient.

The inserted file is Postscript code to control the printer, the more_files
are additional Postscript code to reset the printer.
------------------------------------------------------------------------
Bill Morrow    Clinical Neurosciences, University of Calgary

http://www.cns.ucalgary.ca/~morrow
HMRB 105, 3330 Hospital Drive NW Calgary, Alberta, CANADA T2N 4N1

 
 
 

Adding text to the top of a file without using temporary files

Post by Andrew Sco » Wed, 24 Aug 1994 23:34:26




>> I want to send a text file via "mail". Before I pass the file to mail, I want
>> to insert at the top of the file a subject line of "Subject: < report name>".
>What's about:   mailx -s 'Report Name' <somefile

First, thanks to everyone who helped me out.

In some of the replies, references were made to "mailx". Now, on our NCR
machines (Unix SVR4), we have both "mail" and "mailx". mailx, according to the
manual has a lot more options including a subject line parameter (!) but I was
advised to use "mail" instead. If someone has experience of a similar setup
then all the better, but can anyone shed some light on the differences between
the two commands. I couldn't see any copyright notice in the manual to suggest
that one was a Berkeley (sp) utility or similar so I have been unable to
resolve the differences myself.

Thanks again,
Andrew.
=====

Development Centre, IS.        
AT&T Global Information Solutions,
Kingsway West, Dundee, Scotland  DD2 3XX                
Tel: +44 382 592531  Fax: +44 382 622198

 
 
 

Adding text to the top of a file without using temporary files

Post by The Cola Vampi » Fri, 26 Aug 1994 00:53:13



Andrew> I want to send a text file via "mail". Before I pass the file
Andrew> to mail, I want to insert at the top of the file a subject
Andrew> line of "Subject: < report name>".

Andrew> I would dearly like to do this without using temporary files,
Andrew> etc. but I can't find a way of doing it. I've tried echoing
Andrew> text through pipes, cating the mail file various ways to try
Andrew> and prefix it with the subject line. I've looked at sed but,
Andrew> not being a sed expert, I haven't found a solution that way
Andrew> either.

        err...  Do you want to actually edit the file or just edit it
        to be sent to mail.  If all you want is to prepend
        "Subject:  <whatever>" to the top of the out going message
        then how about

sed "1i\\
Subject: <your text here>" < infile | mail <address>

        If you really want to edit the actual file you might try
        something like the following.

print "1i\nSubject: report name\n.\n1,$\nw!" | ex - infile | mail <address>

        Note:  this assumes korn shell.  if you are not using that you
        will have to make sure you have a version of echo that
        interprets '\n' as a new line.

Andrew> Before anyone asks, there isn't a switch to pass the subject
Andrew> to the mail command. 8-(

        Are you sure?  You may have more than one mail program on
        your system.  Look around.  If you have a mail program with
        a subject switch I think that is probably your best option

Andrew> Does anyone know of a one line solution to do this without the
Andrew> creation of temporary files?

        Well the sed solution technically takes two lines.  Think you
        can live with that?

Andrew> Thanks Andrew Scott.

        You're welcome,
        -Andrew Feren
--

 Cabletron Systems Inc.
 Durham, NH

 
 
 

Adding text to the top of a file without using temporary files

Post by Conrad E. Kimba » Sat, 27 Aug 1994 02:55:27





|> : : In general, adding a line before a file can be done by:
|>
|> : : (echo "fooooo" ; cat file) > somewhere
|> : : (echo "fooooo" ; cat file) | program
|>
|> : Does that work with adding something before an incoming pipe?
|> : I want to insert a file before something that is getting piped
|> : to lpr.
|>
|> : So that I get
|>
|> : my_inserted_file, stdin, more_files
|>
|> : sent to the print queue in that order, as one job. The files have to
|> : remain separate entities, my inserted file is Postscript, and the
|> : stdin could be anything, usually plain text.
|>
|> : You could redirect the stdin into some temporary file, but that is
|> : inefficient.
|>
|> : The inserted file is Postscript code to control the printer, the more_files
|> : are additional Postscript code to reset the printer.
|>
|> Try this:
|>
|>     cat start_file - end_file | lpr
|>
|> cat will interpret a "-" on the command line as "read from stdin".

This is a fine solution for inserting a file before stdin.

If you want to insert and/or append text that isn't already in a file,
then the solution using a subshell, echo, and cat will also work, e.g.:

    source_cmd | (echo "foo"; cat; echo "foo2") | sink_cmd

--
Conrad Kimball           | Client Server Tech Svcs, Boeing Computer Services

(206) 865-6410           | Seattle, WA  98124-0346

 
 
 

1. To use temporary files or not to use temporary files?

This is a repost with Subject line changed:

It is very sad to see that Brian Kernighan and Rob Pike uses temporary
files in the example. I think the temporary can be easily and better
avoided in this case. The modified version is provided at the end. If you
believe the modified version is not better, I would like to hear it.
I make a lot of effort not to use temporary files in my shell scripts,
I do not know if I am alone in this.

And I do not see the value of saving the the original file in the
script:

cp $file $old    # save original file

In "The UNIX Programming Environment", Brian Kernighan and Rob Pike provide

#!/bin/ksh
# overwrite:  copy standard input to output after EOF
# modified version

opath=$PATH
PATH=/bin:/usr/bin

case $# in
0|1)    echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2
esac

file=$1; shift


then
        trap '' 1 2 15   # we are committed; ignore signals
        print -r -- "$new" >  $file
else
        echo "overwrite: $1 failed, $file unchanged" 1>&2
        exit 1
fi

2. XEmacs 19.13/libcurses

3. Append a formfeed character to the end of a text file without adding a new line?

4. How to force ssh tunneling

5. getting rid of top lines of a file without searching through entire file

6. disksuite

7. add file to top of another file

8. STL and trees.

9. Delete lines without creating temporary file?

10. Write to 2 file from pipe without using temp file

11. File locking text file in linux using C

12. Copy files using filenames from text files with shell script or bash script

13. MS-window text file viewer to read a text file in SCO server.