"vi"-ing a constantly updated file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers "vi"-ing a constantly updated file
# 1  
Old 04-25-2007
"vi"-ing a constantly updated file

Hi,

A bit of a dumb question here. Sorry folks.

Suppose I have this file which is being updated at irregular intervals (a few seconds, a few minutes, who knows) by a background process.

I 'vi' this file with the intention of editing it, so it opens up a temp copy as the usual /tmp/720cwhatever

Now suppose, whilst I have this file opened, its updated several times by the background process.

I make my changes, save-quit.

What happens to the updates that were made by the background process during the time I had the file opened?


How do I mitigate this?
(unfortunately, stopping the background process is not an option).


The file is fairly big also (about 40Mb) so it takes vi a few seconds to load it up, save to it etc.....


Unix is ksh.
OS is SunOS.

Hope you guys can help! - thanks in advance!
# 2  
Old 04-25-2007
The updates made by background process are not lost, the question is what happens when you are about to do "save & quit" - you will be prompted with message that the original file has changed : do you want to save anyway ?
The other thing is when someone else is trying to vi the same file opened by you - he will get a message :

Quote:
E325: ATTENTION
Found a swap file by the name ".temp.txt.swp"
owned by: sysgate dated: Wed Apr 25 17:29:23 2007
file name: ~sysgate/temp.txt
modified: no
user name: sysgate host name: $myhost
process ID: 30240 (still running)
While opening file "temp.txt"
dated: Wed Apr 25 17:27:07 2007

(1) Another program may be editing the same file.
If this is the case, be careful not to end up with two
different instances of the same file when making changes.
Quit, or continue with caution.

(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r temp.txt"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".temp.txt.swp"
to avoid this message.

Swap file ".temp.txt.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:
but, I suppose you mean large log files, so the solution will vary depending on what exactly you want to do.
# 3  
Old 04-25-2007
Percy, as sysgate noted, your solution will heavily depend on what exactly
you want to update in the file.
Is it really necessary to "vi" the file?
Could a shell be written to do the updates you want?
# 4  
Old 04-25-2007
OK cheers guys!

A bit more info:

What exactly I want to do is this:

This is a big file, full of filenames and a hashcode for each filename.
each line in the file is
filename, date, hashcode
filename, date, hashcode
...

Anyway, so a file comes into a server, gets a hashcode which is then put into this big log file of mine, and then the file is processed.

Should the file be sent in again to the server, its hashcode is checked and if it exists (ie, my process has dealt with it already) it deals with the file appropriately.

I come unstuck when I need to reprocess the same file again - so I need to edit the large log file and remove the entry (the whole line - name,date,hashcode) so that I can feed it through again.


Write a sh to deal with this? - not sure - its one of those where it happens often, but not often enough to justify a sh....but its something I guess I could look into.


One other thing:

Suppose two people sudos to a user (the same user) first, and then edits the file - you don't get the message about the file changing when you save/quit right? - at least that's what it appears to happen over here in bizarro world...
# 5  
Old 04-25-2007
Percy,
Here is one possible solution:
1) Rename the "work_file" to a "tmp_file" and immediately create a
new "work_file" so that new records can continue to be added to it.
Code:
mv  work_file  tmp_file
touch  work_file

2) "vi" the "tmp_file" and make all changes you want.

2.a) After you are finished with your changes on the "tmp_file",
do not quit yet.

2.b) Go to the end of the "tmp_file" and in the command line type:
Code:
:r work_file

This will read the "work_file" entirely and place it after the last line
of "tmp_file".

2.c) Write and quit the "tmp_file".
As you say the file is very large, if you try to save the work area
as "work_file", it may take a long time and use a lot of memory
and disk space.

3) Immediately rename "tmp_file" back to "work_file".
Code:
mv  tmp_file  work_file

See if this could work for you.
# 6  
Old 04-26-2007
Thank you for this.

There is still the slight potential of missing additions to the logfile between steps 2c and 3 - and its mainly due to how fast I can type "mv file file2" - in a panic, I probably spend more time hitting the backspace than actually typing commands correctly.

Its definitely a better catcher of entries than the solution I had in mind - so thanks very much! Smilie
# 7  
Old 04-26-2007
I also want to point out that your question is not dumb at all, in my previous company we were 7 admins with root access + the process who was doing the updates of sensitive data files. There were moments that we had to edit the file manually, and, fortunately, we had the possibility to rewrite the deamon to accept responces that the files is inaccessible now, and batch the data for later insertion. That's why the question is not stupid, instead may turn into headache.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Permission error when "touch"ing file with different user

Hi, There are 2 users (T886072 & T864764) that need to be provided full (rwx) access to a directory. I made the changes to the directory permissions using chmod and setfacl : root@digidb2:# chmod 700 /u02/ftpfiles/MFRS16/discount_rates/ root@digidb2:# setfacl -s... (3 Replies)
Discussion started by: anaigini45
3 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Ignore "a line" within /etc/profile only when su'ing

Is there a way to ignore a command within /etc/profile only when su'ing as any particular user. For instance.... As root, if I run "su - oracle -c program" it is running a string in my profile called "echo "^2;$(hostname)-(ABC) @ $(who am i)^G". I don't want the above string to run if I... (9 Replies)
Discussion started by: lwif
9 Replies

7. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question