How to remove a temporary file inside gawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove a temporary file inside gawk
# 1  
Old 07-01-2012
How to remove a temporary file inside gawk

Hi-
How can I make the temporary file 0 byte , created inside gawk.

I am using
HTML Code:
system("rm -f temp_orders");
It seems system command is deleting file permanently and I am not able to execute below statement.

print ORD_HEAD_FULL >> cFILE; (cFile is temp_orders)
# 2  
Old 07-01-2012
You should always provide the complete code. If you don't fully understand the problem, you may be omitting something important without realizing it.

Your problem is probably the result of not calling close() on that file. rm only removes the directory entry, but since AWK still has a file descriptor open on the file, the kernel will not remove it. The file still exists, although with no name in the filesystem, and AWK can continue to write to it with those print statements. Use close and then the next time you try to print to that file AWK will open (possibly creating) a new file with the same name.

Alternatively, if you only need to truncate the file, system("rm ...") is unnecessary. The following sequence will truncate:
Code:
close(cFile); printf "" > cFile

Regards,
Alister
# 3  
Old 07-01-2012
I'm not sure I understand what you are trying to accomplish, nor what is happening, so I'm going to guess.

First, I assume that the statement:
Code:
print ORD_HEAD_FULL >> cFILE;

is in your gawk programme, and that when the gawk programme finishes you expect to have temp_orders still existing such that other functions in the script have the data that the gawk produced.

If that's correct, then don't delete the temporary file from the gawk code. Delete it at the end of the script. The short example below does this:

Code:
tmp_data=/tmp/PID$$.data1   # tempoary file for data generated by awk
ps -elf | gawk -v tdata=$tmp_data '
   /UID/ { print; next; }   # print header so it is not sorted
   { print >> tdata }       # cache data for sort later
'

sort -k 3,3 $tmp_data           # sort all non-header data
rm $tmp_data                     # remove tmp file

It's a silly example that could be done without a tmp file, but shows what I mean with regard to deleting the file at the end of the script.

Hope this helps.

---------- Post updated at 11:41 ---------- Previous update was at 11:36 ----------

After reading alister's post (we crossed), I'm not sure I made the right assumptions, but will let it stand.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove trailing space in Gawk

Hi, I have simply made a shell script to convert *.csv to *.xml file. Xml file is required for input to one tool. But i am getting space after last field. How can i remove it. Shell script is as follows :- if then echo "" echo "Wrong syntax, Databse_update.sh... (6 Replies)
Discussion started by: stillrules
6 Replies

2. Programming

Storing a Temporary File Using C

How would someone save a file such as /etc/vpnc/test.conf locally into a temp file, so it can be queried? So for example if I used rsync to copy this file locally, how would I add that to a temp_file variable and discard it using unlink? #include <stdio.h> #include "error.h" ... (15 Replies)
Discussion started by: metallica1973
15 Replies

3. UNIX for Dummies Questions & Answers

Mailx - temporary mail message file: No such file or directory

How would I go about resolving this error temporary mail message file: No such file or directory Can anybody tell me where the default location is for the temporary mail message file is for mailx? It appears that it doesn't exist. Thanks (1 Reply)
Discussion started by: joen
1 Replies

4. Shell Programming and Scripting

Remove everything inside of brackets

I need to use something bash related to remove everything inside of brackets. For example. In the following: abc<def>ghi<jkl>mno the result should be: abcghimno (4 Replies)
Discussion started by: locoroco
4 Replies

5. Shell Programming and Scripting

gawk to remove last character in a line or string

I am outputting a line like this print $2 "/" $4The last character though is a ":" and I want to remove it. Is there any neat way to remove it? Or am I forced to do something like this: print $2 "/" substr($4, 1, length($4) - 1)Thanks. (6 Replies)
Discussion started by: benalt
6 Replies

6. Shell Programming and Scripting

How to remove the first line of a file without using any temporary file

I want remove first line of the file without using any temporary file. Everything should be done in the original file itself since I don't any file creation access whereas I have modify access. It would be great if somebody help in this regard ASAP . Thanks in Advance. Kishore:mad: (1 Reply)
Discussion started by: tvbhkishore
1 Replies

7. Shell Programming and Scripting

To remove file using rm inside c

Unixians, I need one help,I have to remove a file from particular path. see my code snippet, { char cmd=""; sprintf (cmd, "/bin/rm -f %s%s%s%s","/usr1/mydir/", 1.t,2.t,3.t); system(cmd); } The problem is it read as a "/usr1/mydir/1.t2.t3.t" and no files is removed. Can u plz... (7 Replies)
Discussion started by: kkl
7 Replies

8. Shell Programming and Scripting

Perl : how to modify a file without generate a temporary file

Hi All, I have a file like below, how can i insert one line after line 1 without using a temporary file in perl? line 1 line 2 line 3 expected result line 1 new line <---insert here line 2 line 3 (2 Replies)
Discussion started by: summer_cherry
2 Replies

9. Linux

How to remove only html tags inside a file?

Hi All, I have following example file i want to remove all html tags only, Input File: <html> <head> <title>Software Solutions Inc., </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor=white leftmargin="0" topmargin="0"... (2 Replies)
Discussion started by: btech_raju
2 Replies

10. Shell Programming and Scripting

remove temporary file ?

Hi all, In the script I am creating a temporary file with process id as temp.txt.$$ I want to remove this tomporary file first from the current directory when i'll run the same script next time. Note: Every time when the script executes then it has unique process id and it'll create a... (5 Replies)
Discussion started by: varungupta
5 Replies
Login or Register to Ask a Question