Link to home
Start Free TrialLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

reading from a file and writing contents back to it

Hi Experts,

I want to wait for a file to be written with some data and remove the contents from that file and write back to it.  How can I do it?

Thanks
Avatar of Cong Minh Vo
Cong Minh Vo
Flag of Viet Nam image

You can open file with r+ or w+ to read, write and modify content.
http://www.go4expert.com/forums/showthread.php?t=2977
and fseek() func
http://www.cplusplus.com/reference/clibrary/cstdio/fseek/
1. If you know the unique pattern of the content, then you can do
grep   <PATTERN>   file
   to check the output is the content that you want to remove.
  If the output is exactly those content you want to remove, then
sed '/PATTERN/d'   file > new_file
mv   new_file   file


2. If you know the unique pattern in the first line of the content to the end of file, then you can do
sed '/PATTERN/,$d'   file   > new_file
mv  new_file   file


3. If you know the line number of the content you want to remove, say line 21 ~ 25, then you can do
sed   '21,25d'   file  > new_file
mv  new_file   file
Avatar of ambuli

ASKER

Thanks all. Sorry for not being clear. Actually I am trying to code what
the following commands do

#exec 3<> /ambuli/myfilel
#echo "msg:some-message" >&3; cat <&3

My code has to wait until something is written to the file and then remove the contents and write back to it.
Thank you.
> My code has to wait until something is written to the file and then remove the contents and write back to it.
Then don't write to it. Take out
echo "msg:some-message" >&3
ASKER CERTIFIED SOLUTION
Avatar of xeltek
xeltek
Flag of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ambuli

ASKER

I know it sounds silly, but I need to remove the data and write it back.  This is for a test.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial