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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output redirection to top of file

Status
Not open for further replies.

irixfan

IS-IT--Management
May 14, 2002
57
FI
I have a csh script that gives me a text line as output.
I can add this line to the end of a file by using >>
How to add the line to be the first line of the file?

Now I use a temporary file for output and join the files as separate command to get the last output be the first line. That works but I want the script as simple as possible.
 
Don't know about csh..
This idea will work.
Code:
function insert() { 
line=${1:-""}
file=$2

   f=0
   while read new
   do     
      if [ $f -eq 0 ] ; then
          echo $line     
      fi
   echo $new
   f=`expr $f + 1`
   done < $file > /tmp/tmp && mv /tmp/tmp $file
}
 
Hello,
Try this
general format:
output of the csh script|cat - file_used|tee file_used.


for ex:if the command of your script islike this

var=&quot;hello&quot;
echo $var---say this is the output
then

use this:

echo $var|cat - file_used|tee file_used

if file_used say contains = &quot;world&quot;


then ouput redirected in same file
is

hello
world


cheers
Santosh.K.B

 
Thanks, I see the idea.
But I can't get it working.
echo &quot;new_line&quot;|cat - log.txt|tee log.txt
may work ok once or twice, then it gives me only one line - or lot of lines, and then again only one line. The output seems to be random.
What's wrong?
 
Hello,

This command which i have given doesnot redirect the some of the syntact errors in script output.

that is why whenever there is some kind of error there the log.txt doenot contain any output.

do this:
1>make your script to show correct output.
2>give the executable permission for your script file
3>use the command
yourscript.sh|cat - log.txt|tee log.txt

give proper space between cat,-,log.txt

Any way this is working me fine.I donot know how you are getting lot of lines.

Regards
SANTOSH.K.B


 
tee - replicate the standard output

echo &quot;$var&quot; 2>&1 |cat - file_used|tee -a file_used vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
HEllo,
As you said it the output is not consistent with some of commands and their options.
AS a observed ls -l|cat - log.txt|tee log.txt isnot working
for me,as it is overwritting the contents of file instead of add at beg.it seems there is some limit for line above which it is not working prop(this is guess not sure)
but i will look for that.

 
If you read and write the same file in the same pipeline, you'll be stepping on yourself and occasionally overwriting what you're reading. Try the following...
[tt]
echo &quot;New_line&quot; | cat - log.txt > log.tmp ; mv log.tmp log.txt
[/tt]
This uses a temp file, but it's cleaner functionally. The only problem you might have is if a lot of processes are trying to do the same thing to the same file. They would really be stepping on each other. Of course the other solutions have this problem too.
 
OK guys, thanks for confirming me that my original idea of using a temporary file is the best solution. Actually, now I understand that the syntax of the command line (from SamBones) was what I was looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top