ambuli
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
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
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
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
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.
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
Then don't write to it. Take out
echo "msg:some-message" >&3
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I know it sounds silly, but I need to remove the data and write it back. This is for a test.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.go4expert.com/forums/showthread.php?t=2977
and fseek() func
http://www.cplusplus.com/reference/clibrary/cstdio/fseek/