0

I am working with a text file, which contains a list of processes under my programs control, along with relevant data.

At some point, one of the processes will finish, and thus will need to be removed from the file (as its no longer under control).

Here is a sample of the file contents (which has enteries added "randomly"):

PID=25729 IDLE=0.200000 BUSY=0.300000 USER=-10.000000
PID=26416 IDLE=0.100000 BUSY=0.800000 USER=-20.000000
PID=26522 IDLE=0.400000 BUSY=0.700000 USER=-30.000000

So for example, if I wanted to remove the line that says PID=26416.... how could I do that, without writing the file over again?

I can use external unix commands, however I am not very familiar with them so please if that is your suggestion, give an example.

Thanks!

7 Answers 7

1

Either you keep the contents of the file in temporary memory and then rewrite the file. Or you could have a file for each of the PIDs with the relevant information in them. Then you simply delete the file when it's no longer running. Or you could use a database for this instead.

1

As others have already pointed out, your only real choice is to rewrite the file.

The obvious way to do that with "external UNIX commands" would be grep -v "PID=26416" (or whatever PID you want to remove, obviously).

Edit: It is probably worth mentioning that if the lines are all the same length (as you've shown here) and order doesn't matter, you could delete a line more efficiently by copying the last line into the space being vacated, then shorten the file so eliminate what had been the last line. This will only work if they really are all the same length though (e.g., if you got a PID of '1', you'd need to pad it to the same length as the others in the file).

0

The only way is by copying each character that comes after the deleted line down over the characters that are deleted.

It is far more efficient to simply rewrite the file.

0

how could I do that, without writing the file over again?

You cannot. Filesystems (perhaps besides more esoteric record based ones) does not support insertion or deletion.

So you'll have to write the lines to a temporary file up till the line you want to delete, skip over that line, and write the rest of the lines to the file. When done, rename/copy the temp file to the original filename

0

Why are you maintaining these in a text file? That's not the best model for such a task. But, if you're stuck with it ... if these lines are guaranteed to all be the same length (it appears that way from the sample), and if the order of the lines in the file doesn't matter, then you can write the last line over the line for the process that has died and then shorten the file by one line with the (f)truncate() call if you're on a POSIX system: see Jonathan Leffler's answer in How to truncate a file in C?

But note carefully netrom's answer, which gives three different better ways to maintain this info.

Also, if you stick with a text file (preferably written from scratch each time from data structures you maintain, as per netrom's first suggestion), and you want to be sure that the file is always well formed, then write the new data into a temp file on the same device (putting it in the same directory is easiest) and then do a rename() call, which is an atomic operation.

0

You can use sed:

sed -i.bak -e '/PID=26416/d' test

-i is for editing in place. It also creates a back-up file with the new extension .bak

-e is for specifying the pattern. The /d indicates all lines matching the pattern should be deleted.

test is the filename

0

The unix command for it is:

grep -v "PID=26416" myfile > myfile.tmp

mv myfile.tmp myfile

The grep -v part outputs the file without the rows with the search term.

The > myfile.tmp part creates a new temp file for this output.

The mv part renames the temp file to the original file.

Note that we are rewriting the file here, and moreover, we can lose data if someone write something to file between the two commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.