Operating System - HP-UX
1837078 Members
2540 Online
110112 Solutions
New Discussion

Re: Insert at top of file?

 
Ian McClement_1
Occasional Advisor

Insert at top of file?

Hi
Is there a way to redirect output to the top of a file, like >> but to go to top, without removing the file, inserting, then appending the old contents?

Any suggestions?

Thanks
Ian
9 REPLIES 9
Vincenzo Restuccia
Honored Contributor

Re: Insert at top of file?

#top -f file_name
federico_3
Honored Contributor

Re: Insert at top of file?

If i have two files ( let's say file1 and file2 ) and i want write file1 at the top of file2 i could use the following:

cat file1 file2 > file3
CHRIS_ANORUO
Honored Contributor

Re: Insert at top of file?

Create a file appfile, then append the newfile to the newly created file

i.e copy file to appfile
then newfile >> appfile
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Ian McClement_1
Occasional Advisor

Re: Insert at top of file?

Sorry, what I meant was if I have a file

firstline
secondline
thirdline

I would like to redirect the output of a command to the top of this file resulting in

textoutput
firstline
secondline
thirdline

Thanks
Ian
Kofi ARTHIABAH
Honored Contributor

Re: Insert at top of file?

Ian:

to achieve what you want, you must have an intervening/temporary files eg.:

echo $STRING > /tmp/prepend ; cat /tmp/prepend $YOURFILE > /tmp/finalfile ; mv /tmp/finalfile $YOURFILE ; rm /tmp/prepend

you could actually alias the above string (or some other incarnation of it)

good luck
nothing wrong with me that a few lines of code cannot fix!
Abel Berger
Regular Advisor

Re: Insert at top of file?

Hi all,

I don?t remember, but maybe this its possible
whith ed command.

I hope this helps

Abel Berger
federico_3
Honored Contributor

Re: Insert at top of file?


If you have file1:

firstline
secondline
thirdline

redirect the output of a command to a file
output > file2

and then:
cat file2 file1 > file3

you'll see in file3:

textoutput
firstline
secondline
thirdline

Re: Insert at top of file?

Hi Ian,

Here's another simple way
logFile.new contains:
firstLine
secondLine
thirdLine

One way could be :

mv logFile.new logFile.old
yourcommand > logFile.new | cat logFile.old >> logFile.new

logFile.new now contains:

firstLine
secondLine
thirdLine

But then again, tell us what is it you intend to do.

There might be an easier way using the "tail" command.

regards,
Fritz
If it ain't rough it ain't me
Curtis Larson_1
Valued Contributor

Re: Insert at top of file?

#!/usr/bin/ksh

ex -s yourfile <0 r !yourSystemCommand
wq
EOF