Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

prepending text to multiple files

Status
Not open for further replies.

bi

Technical User
Apr 13, 2001
1,552
0
0
US
I know to append text at the end of a file, you can use echo "text text" >> file

What if I want the new text to be placed at the beginning of the file, rather than the end?
 
Hi:

How about placing "some text" in a file, call it:

echo "some text" > sometext.txt

cat datafile >> sometext.txt

Regards,


Ed
 
Of course. Sometimes the most obvious solutions are the hardest to see.

Thanks.
 
Hello Bi,
I prefer to use ex in this instance where the text must go at the beginning of the file AND the file must be saved with the same name when completed.

Suppose the insert text is a file named Insert.txt and must go into Test.log. A command file for ex must also be created and I'll call it Command.ex.

The code would look like this:

echo "0r Insert.txt" > Command.ex
echo "x" >> Command.ex
ex Test.log < Command.ex

The &quot;x&quot; is to exit and save the changes. Remember that ex is meant to be interactive, but can be fooled with an input file redirection.

Suppose you want the result of a command at the beginning of the file. For simplicity, I'll use date. The command code would look like this:

echo &quot;0r !date&quot; > Command.ex
echo &quot;x&quot; >> Command.ex
ex Test.log < Command.ex

The beauty of ex is that it will manage the target file the same as if you were using vi, the drawback being that you must build a command file to direct to it.
The cases where I most use the ex technique are configuration file maintenance or a similar condition where the modified file must keep its original name.
Have fun!
Charlie Ehler
 
Hi:

You can do something similar with vi and a unix here document, although you have a problem cutting and pasting:

vi file > /dev/null 2>&1 <<MSG
Osome text ^[
:wq
MSG
# end script

Here's what happens:
1) Edit the file with vi
2) Since you're at the beginning the file, the O command opens a line at the top.
3) enter &quot;some text&quot;
4) leave edit mode by pressing escape control character. When you enter this in vi, press control-v and then the escape character
5) Press :wq to quit the script.

Regards,

Ed

 
Thanks, Charlieehler and olded. I'll try all these methods. I've been trying to hone my scripting skills and I'm grateful there is a forum like this that can answer my questions.
 
I, too, need this ability to keep a running lab notebook. I desire that the latest entries be at the top so that the users don't have to scroll to make or read an entry.
I tried the solutions mentioned here, but to no avail.
I am most interested in the vi sol'n, but as written I get an illegal redirection. I also couldn't execute it when I tried writing the commands to a separate file (>>) even with an echo for #!/bin/sh at the top.
Could I please get a little more clarification on how to implement this...currently I am adding the Date and $USER to the file, but it is appending at the bottom. I'd like to do the same but reverse the chronology of the entries.

 
Duane:

This script:

#!/bin/sh

vi file > /dev/null 2>&1 <<MSG
O`date`
`echo $USER`^[
:wq
MSG
# end script

The above command using the Unix &quot;here&quot; document
1) vi file and enter editing mode by opening the top of the file with uppercase O
2) execute the date command with back-ticks - command substitution.
3) ditto with echoing $USER
4) exit the edit mode by pressing the escape key. To emualte this when creating the script with vi, press control-v and press the ESC key.
5) Exit vi with :wq

Regards,

Ed
 
Ed:
Thank you for your reply.
I understand the `ticks` for execution, suppressing the terminal, & the vi commands obviously make sense, but I am at a lost with &quot;... 2>&1 <<MSG &quot;
- I don't fully understand what's happening -and- &quot;MSG&quot; is a mystery to me. also, what do you mean when you say &quot;using the Unix &quot;here&quot; document&quot; (specifically by &quot;here&quot;)
Duane

-=-=-
 
Duane:

>but I am at a lost with &quot;... 2>&1 <<MSG &quot;
>- I don't fully understand what's happening -and- &quot;MSG&quot; >is a mystery to me. also, what do you mean when you >say &quot;using the Unix &quot;here&quot; document&quot; (specifically >by &quot;here&quot;)

1) 2>&1 redirects file hand 2 which is standard error to standard input, file handle 1. In this case, right before 2>&1 standard input is redirected to /dev/null. Remove the redirections for file handle 1 and 2 and you can see the output.

2) Instead of getting input from the keyboard or redirecting input from a file, you can embed input inline in a script using << and some arbitrary word. The here document terminates with the same arbitrary word. It's important that the ending word exist and be in column 1.

A simple example:

Instead of echo'ing lines of text:

echo &quot;line 1&quot;
echo &quot;line 2&quot;
echo &quot;line 3&quot;

use a &quot;here document:

cat << MSG
line 1
line 2
line 3
MSG

I'm told it's called a &quot;here&quot; document, because you get the input &quot;here&quot; vs. from over &quot;there&quot; i.e. a keyboard or file.

My example uses the &quot;here&quot; document to provide the input instead of a user entering the same keystrokes from the keyboard. You can do this with nearly any unix command.

Let me know if you have any other questions.

Regards,

Ed
 
Ed:
Thanks, that was most helpful!
I am going to tinker with my script using your tips & I'll post it back to see what you think.
I appreciate your help,
Duane
 
Another approach is to just append to the file in the usual way, ie.

read $LINE
echo $LINE >> logfile

but when you want to view the logfile in reverse order, use

sed '1!G;h;$!d' logfile

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top