Thanks for the question, Raju.
Asked: January 06, 2016 - 11:06 am UTC
Last updated: April 09, 2016 - 7:02 am UTC
Version: Oracle 11g R2
Viewed 10K+ times! This question is
You Asked
Hi,
I'm creating a new file using PUT_LINE procedure of UTL_FILE package. This inserts an empty new line at the end of the file.
Can you please suggest me how to delete this last line from the file?
I don't want the last empty line to be included in my file. Also my file is a .csv file and of size of 2-3 GB. Hence, suggest me the suitable process to remove the last empty line without hampering the performance.
Following is a sample:
declare
file UTL_FILE.file_type;
fileData VARCHAR2(2096):='This is the test line'; -- File Content
fileName VARCHAR2(50):= 'Test File.txt';
path VARCHAR2(2096):='FLS03_DIR_CARD_REQ'; -- Oracale Directory Name
begin
file := utl_file.fopen(path,fileName,'W');
utl_file.put_line(file, fileData);
utl_file.put(file,'Next Line Test');
IF (utl_file.is_open(file)) THEN
utl_file.fclose(file);
END IF;
end;
The above test procedure creates a file with name "Test File.txt" and writes a single inside it. After writing that line, a new line character is getting appended in the file. I simply don't want that extra new line at the end.
My code has been implemented with the above logic of file writing. However, in my code, the number is lines may became 10 millions depending upon the business logic.
Hence, can you please let me know how to remove the last empty line at the end of file.
Kindly find the information as requested.
The actual output I am getting by using the above code is:
Line 1: This is the test line
Line 2:
Here, after writing the first line (i.e, Line 1), a new line character is getting written i.e., Line 2, though nothing has been written on this line. I don't want to write any extra line without any content on it. I need the below outcome:
Line 1: This is the test line
In my code, after writing the file we are sending the file to another system. In which the file gets loaded by reading the content of the file. Now, in that system, they are expecting content in every line. However, at the end of the file, they are getting a line but no content, which causes an error.
Hence, please suggest me any solution to write a line in a file which terminates without any new line.
and Connor said...
With standard utl_file (ie, text mode) *we* control the end of line semantics. Hence as you've seen, we are in charge of CR and LF, eg
SQL> declare
2 file UTL_FILE.file_type;
3 fileData VARCHAR2(2096):='This is the test line'; -- File Content
4 fileName VARCHAR2(50):= 'TestFile.txt';
5 path VARCHAR2(2096):='TEMP'; -- Oracale Directory Name
6 begin
7 file := utl_file.fopen(path,fileName,'W');
8 utl_file.put_line(file, fileData);
9 utl_file.put(file,'Next Line Test');
10 IF (utl_file.is_open(file)) THEN
11 utl_file.fclose(file);
12 END IF;
13 end;
14 /
PL/SQL procedure successfully completed.
C:\temp>od -c TestFile.txt
0000000 T h i s i s t h e t e s t
0000020 l i n e \r \n N e x t L i n e
0000040 T e s t \r \n
0000047
If *you* want total control over the file, then you access the file in the raw manner, eg
SQL> declare
2 file UTL_FILE.file_type;
3 fileData VARCHAR2(2096):='This is the test line'; -- File Content
4 fileName VARCHAR2(50):= 'TestFile.txt';
5 path VARCHAR2(2096):='TEMP'; -- Oracale Directory Name
6 crlf varchar2(2) := chr(10)||chr(13);
7 begin
8 file := utl_file.fopen(path,fileName,'wb');
9 utl_file.put_raw(file, utl_raw.cast_to_raw(fileData||utl_Tcp.crlf));
10 utl_file.put_raw(file,utl_raw.cast_to_raw('Next Line Test'));
11 IF (utl_file.is_open(file)) THEN
12 utl_file.fclose(file);
13 END IF;
14 end;
15 /
PL/SQL procedure successfully completed.
C:\temp>od -c TestFile.txt
0000000 T h i s i s t h e t e s t
0000020 l i n e \r \n N e x t L i n e
0000040 T e s t
0000045
Rating
(3 ratings)
Is this answer out of date? If it is, please let us know via a Comment