MLUG: Re: [MLUG] prepending a file
Re: [MLUG] prepending a file
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
>>>>> "Brent" == Brent Deterding <EMAIL:PROTECTED> writes:

Brent> I want to add some text to the top of a file - how do I do it?
Brent> Like >> but at the beginning, not the end

Yummy shell scripting Trix.  Just add milk. :-)  

cat allows the use of '-' as one of the 'files' to dump to stdout. So,
have fun with input redirection and make sure that the '-' appears
before the bottom file name.

$ cat bottom.txt
top
middle
bottom

==> Using Here documents:

$ cat <<EOT - bottom.txt > new.txt
> new top
> EOT
$ cat new.txt
new top
top
middle
bottom

==> using stdin from a pipe

$ echo "new top" | cat - bottom.txt > new.txt
$ cat new.txt
new top
top
middle
bottom

==> using stdin from file
$ cat - bottom.txt < top.txt > new.txt
$ cat new.txt
new top
top
middle
bottom

==> using stdin from keyboard
$ cat - bottom.txt > top.txt
new top
^D
$ cat new.txt
new top
top
middle
bottom


==> Or you can have some esoteric fun.  Although this style does pass
    through bottom.txt twice.  And you can do all the other input       
    redirection things above in this style too.

$ echo "new top" | tac bottom.txt - | tac > new.txt
$ cat new.txt
new top 
top
middle
bottom


enjoy,

-jeremy


-- 
========================================================================
 Jeremy Hinegardner                              EMAIL:PROTECTED 


_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members