Sponsored Content
Top Forums Shell Programming and Scripting Removing lines from a file being used by another process using SHELL script Post 302705573 by Lem on Tuesday 25th of September 2012 06:47:37 AM
Old 09-25-2012
Are you in linux? Try flock command. Beware: file locks are NOT mandatory, so every command accessing the file must do it through flock.

Here is an example:
Code:
$ cat file
25/08/2012
31/08/2012
4/09/2012
5/10/2012

$ flock file -c "sed '1,/\/08\/2012/ d' <file >tmpfile; cat tmpfile >file" & flock file -c "echo yadayadayada |tee -a file >/dev/null"
[1] 22160
[1]+  Completato              flock file -c "sed '1,/\/08\/2012/ d' <file >tmpfile; cat tmpfile >file"

$ cat file
4/09/2012
5/10/2012
yadayadayada

To have a real feel of what's happening, reset the original file content and use some sleeps, as in:
Code:
flock file -c "sed '1,/\/08\/2012/ d' <file >tmpfile; cat tmpfile >file; sleep 10" & \
sleep 3; echo "sed has worked; cat has worked, here is the file:"; cat file; \
echo; echo "waiting the last 7 seconds for tee..."; \
flock file -c "echo yadayadayada |tee -a file >/dev/null"; \
echo "tee has been able to write just now. Here is the file:"; cat file

To sum it up:

a) to delete your old lines, use flock file -c "sed ... <file >tmpfile; cat tmpfile >file";

b) prepend flock file -c "... to your tee -a file command accessing the file.
--
Bye
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i know file using another process on lunix shell script?

hi all, I did RedHat OS. How can i know myfile using another process on lunix shell script? Have you got any suggestions? (1 Reply)
Discussion started by: Tlg13team
1 Replies

2. Shell Programming and Scripting

help removing characters for output file in shell script

hi i'm new to shell scripts and have a small problem i am running a batch converter that returns all flash .flv files in a directory and create a png image from each one the problem i have is the $1 variable , its ok on the first call but on the secound call $1.png , i have extra... (1 Reply)
Discussion started by: wingchun22
1 Replies

3. Shell Programming and Scripting

Removing the entire file contents using unix shell script.

I need to remove the entire file contents in file using the shell script. Actually the grap -v command will create one more file and it occupy the space also. I need to remove the entire file contents without creating new file using the shell scripting. Please help me. (5 Replies)
Discussion started by: praka
5 Replies

4. Shell Programming and Scripting

Script for Removing Lines from File / Blocking internet connection

Hey all. I am trying to write some scripts and need some assistance. One: I already have a script that appends lines to a file. I need a script that will remove those lines from that file, and have no idea how to go about doing this. Just need the command (if any) that can remove lines. ... (2 Replies)
Discussion started by: Dysruption
2 Replies

5. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

6. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

7. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

8. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

9. UNIX for Beginners Questions & Answers

By pass a process in a Shell Script on file size

I wish to by pass a process if the file is over a certain size? not sure this makes sense current bit of the script below #if we are bypAssing the OCR if ; then echo Bypassing HOTFOLDER OCR HOT_FOLDER_DIR=$BATCH_POST_OCR_DIR; potential change below? would this work would I need... (1 Reply)
Discussion started by: worky
1 Replies

10. Shell Programming and Scripting

Script to process a list of items and uncomment lines with that item in a second file

Hello, I have a src code file where I need to uncomment many lines. The lines I need to uncomment look like, C CALL l_r(DESNAME,DESOUT, 'Gmax', ESH(10), NO_APP, JJ) The comment is the "C" in the first column. This needs to be deleted so that there are 6 spaces preceding "CALL".... (7 Replies)
Discussion started by: LMHmedchem
7 Replies
tmpfile(3)						     Library Functions Manual							tmpfile(3)

NAME
tmpfile - Creates a temporary file LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <stdio.h> FILE *tmpfile ( void ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: tmpfile(): XSH4.2 Refer to the standards(5) reference page for more information about industry standards and associated tags. DESCRIPTION
The tmpfile() function creates a temporary file and returns its FILE pointer. The file is opened for update. The temporary file is automat- ically deleted when the process using it terminates. RETURN VALUES
Upon successful completion, the tmpfile() function returns a pointer to the stream of the file that is created. Otherwise, it returns a null pointer and sets errno to indicate the error. ERRORS
The tmpfile() function sets errno to the specified values for the following conditions: A signal was caught during tmpfile(). OPEN_MAX file descriptors are currently open in the calling process. [Tru64 UNIX] All available file descriptors are currently open in the calling process. The maximum allowable number of files is currently open in the system. The directory or file system that would contain the new file cannot be expanded. RELATED INFORMATION
Functions: fopen(3), mktemp(3), tmpnam(3), unlink(2), getdtablesize(2) Standards: standards(5) delim off tmpfile(3)
All times are GMT -4. The time now is 09:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy