PLSQL text file creating line ending format - oracle

I've created a text file in some directory with "UTL_FILE.fOpen" method. Actually text file has been created. File line endings is Unix/LF format. But i want create with Windows CR/LF line ending type.
can you give me an any idea?
f := UTL_FILE.fOpen('UTL_FILE_DIR', pFileName, 'w', pLineSize);
pLine := pLine || pColValue;
DBMS_SQL.close_cursor(pCur);
UTL_FILE.fclose(f);

I found my solution here http://knoworacle.blogspot.com/2012/06/oracle-utlfile-with-examples.html and put the chars(chr(13) || chr(10)) at line ends.

Related

How much should the minimum size difference between readbuffer and max_linesize while using UTL_FILE in SQL

What should be the minimum difference between the readbuffer variable size and the max_linesize(specified in UTL_FILE.FOPEN) so the code executes without ORA-29284 error.
For example below is my code:
set serveroutput on;
Declare
f UTL_FILE.FILE_TYPE;
r varchar2(1220);
begin
f:= UTL_FILE.FOPEN('XYZ_DIR', 'ABC.txt', 'R',max_linesize => 1222 );
UTL_FILE.GET_LINE(f,r);
UTL_FILE.FCLOSE(f);
dbms_output.put_line(r);
end;
When I execute the above mentioned code, I get ORA-29284 error. This error is due to the fetched linesize being larger than read buffer in this problem's context.
My file ABC.txt has multiple lines having individual length of 1221 characters. For this scenario getting the 29284 error is valid. But when I strip the line size in file to 1220 character, I still get the same error. If I strip the individual line size in file to 1218, my code works properly.
I want to know the reason why my code fails even though if I strip the individual line in file to 1220 character(same as read buffer) and why do I have to strip the length of lines to 1218 character?

Reading a specific line in a .txt file using Visual Basic 6

I am currently working on an EDI file and now I want to load and read only a specific line on a .txt file and put that in a textbox. Here is what my data would look like
ISA*00* *00* *01*MKT71 *01*ADEV04 *160331*1001*U*00501*300000001*0*P*>~
GS*IN*MKT71*ADEV04*20160331*1001*300000001*X*005010~
ST*810*300000001~
BIG*20160316*BS13435**NONE~
REF * BM * DHL-2162693540~
REF*CR*5201~
Now I want to put only the data DHL-2162693540 located in line number 5 of the .txt file in my Textbox1.
The easiest and most straightforward way to do this is to read the first however many lines, and only keep the value of the last line read.
Here is some code that accomplishes that:
Private Sub ReadLines()
Dim MyLine As String
Dim DataLineNumber As Integer
DataLineNumber = 5
Open "C:\YourFileNameHere.txt" For Input As DataFile
Do While Not EOF(1) And LineNumber <= DataLineNumber
Line Input #DataFile, MyLine
DataLineNumber = DataLineNumber + 1
Loop
Close DataFile
MsgBox MyLine
End Sub

Ruby question mark in filename

I have a little piece of ruby that creates a file containing tsv content with 2 columns, a date, and a random number.
#!/usr/bin/ruby
require 'date'
require 'set'
startDate=Date.new(2014,11,1)
endDate=Date.new(2015,9,1)
dates=File.new("/PATH_TO_FILE/dates_randoms.tsv","w+")
rands=Set.new
while startDate <= endDate do
random=rand(1000)
while rands.add?(random).nil? do
random=rand(1000)
end
dates.puts("#{startDate.to_s.gsub("-","")} #{random}")
startDate=startDate+1
end
Then, from another program, i read this file and create a file out of the random number:
dates_file=File.new(DATES_FILE_PATH,"r")
dates_file.each_line do |line|
parts=line.split("\t")
random=parts.at(1)
table=File.new("#{TMP_DIR}#{random}.tsv","w")
end
But when i go and check the file i see 645?.tsv for example.
I initially thought that was the line separator in the tsv file (the one containing the date and the random) but its run in the same unix filesystem, its not a transaction from dos to unix
Some lines from the file:
head dates_randoms.tsv
20141101 356
20141102 604
20141103 680
20141104 668
20141105 995
20141106 946
20141107 354
20141108 234
20141109 429
20141110 384
Any advice?
parts = line.split("\t")
random = parts.at(1)
line there will contain a trailing newline char. So for a line
"whatever\t1234\n"
random will contain "1234\n". That newline char then becomes a part of filename and you see it as a question mark. The simplest workaround is to do some sanitization:
random = parts.at(1).chomp
# alternatively use .strip if you want to remove whitespaces
# from beginning of the value too

UTL_SMTP.WRITE_DATA - returning double DOTs in Oracle

I am trying to send the below format content as a mail attachment in oracle.
ADV|ESCROW|514838|20130823
CRN|SF|20130727|20130823|20130823|0.00|-25.28|0.00|0.00|0.00|-25.28
CCT|B|0.00|*
CCT|E|0.00|*
CCT|I|0.00|*
CCT|N|0.00|*
CCT|R|0.00|*
CCT|S|0.00|*
CRN|SF|20130726|20130823|20130823|0.00|35638.70|0.00|0.00|0.00|35638.70
CCT|B|0.00|*
CCT|E|0.00|*
CCT|I|0.00|*
CCT|N|0.00|*
CCT|R|0.00|*
CCT|S|0.00|*
PRN|SF|20130725|20130822|0.00
PCT|B|0.00|*
PCT|E|0.00|*
PCT|I|0.00|*
PCT|N|0.00|*
PCT|R|0.00|*
PCT|S|0.00|*
CRN|SF|20130725|20130823|20130822|0.00|1672.95|0.00|0.00|0.00|1672.95
CCT|B|0.00|*
CCT|E|0.00|*
CCT|I|0.00|*
CCT|N|0.00|*
CCT|R|0.00|*
CCT|S|0.00|*
These contents are huge in size. Its almost 109250 characters. So i used the below code to send this text.
l_offset number := 1;
l_amount number := 1500;
while l_offset < dbms_lob.getlength(l_in_mail_attach) loop
utl_smtp.write_data(smtp_connection,
dbms_lob.substr(l_in_mail_attach,l_amount,l_offset));
l_offset := l_offset + l_amount ;
l_amount := least(1900,dbms_lob.getlength(l_in_mail_attach) - l_amount);
end loop;
I have received the attchment in mail succesfully. But the issue is a dot (.) is appended in the text. See below text PCT|R|0..00|* this should be PCT|R|0.00|* .
PRN|R1|20130606|20130819|0.00
PCT|B|0.00|*
PCT|B|0.00|*
PCT|E|0.00|*
PCT|E|0.00|*
PCT|I|0.00|*
PCT|I|0.00|*
PCT|N|0.00|*
PCT|N|0.00|*
PCT|R|0..00|*
PCT|R|0.00|*
PCT|S|0.00|*
PCT|S|0.00|*
Can any one advice how to fix this issue.
The dot duplication occurs when you have a leading dot at the beginning of one of your substrings.
According to:
http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/u_smtp.htm#ARPLS71529
The application must ensure that the contents of the body parameter conform to the MIME(RFC822) specification.
The DATA routine terminates the message with a . sequence (a single period at the beginning of a line),
as required by RFC821. It also translates any sequence of . (single period) in the body
to .. (double period). This conversion provides the transparency as described in Section 4.5.2 of RFC821.
Try hiding the dot from the dot duplicator when writing message body and attachments by replacing:
utl_smtp.write_data(smtp_connection,
dbms_lob.substr(l_in_mail_attach,l_amount,l_offset));
with:
utl_smtp.write_RAW_data
( smtp_connection
, utl_raw.cast_to_raw( dbms_lob.substr ( l_in_mail_attach, l_amount, l_offset ) )
);

How to write some value to a text file in ruby based on position

I need some help is some unique solution. I have a text file in which I have to replace some value based on some position. This is not a big file and will always contain 5 lines with fixed number of length in all the lines at any given time. But I have to specficaly replace soem text in some position only. Further, i can also put in some text in required position and replace that text with required value every time. I am not sure how to implement this solution. I have given the example below.
Line 1 - 00000 This Is Me 12345 trying
Line 2 - 23456 This is line 2 987654
Line 3 - This is 345678 line 3 67890
Consider the above is the file I have to use to replace some values. Like in line 1, I have to replace '00000' with '11111' and in line 2, I have to replace 'This' with 'Line' or any require four digit text. The position will always remain the same in text file.
I have a solution which works but this is for reading the file based on position and not for writing. Can someone please give a solution similarly for wrtiting aswell based on position
Solution for reading the file based on position :
def read_var file, line_nr, vbegin, vend
IO.readlines(file)[line_nr][vbegin..vend]
end
puts read_var("read_var_from_file.txt", 0, 1, 3) #line 0, beginning at 1, ending at 3
#=>308
puts read_var("read_var_from_file.txt", 1, 3, 6)
#=>8522
I have also tried this solution for writing. This works but I need it to work based on position or based on text present in the specific line.
Explored solution to wirte to file :
open(Dir.pwd + '/Files/Try.txt', 'w') { |f|
f << "Four score\n"
f << "and seven\n"
f << "years ago\n"
}
I made you a working sample anagraj.
in_file = "in.txt"
out_file = "out.txt"
=begin
=>contents of file in.txt
00000 This Is Me 12345 trying
23456 This is line 2 987654
This is 345678 line 3 67890
=end
def replace_in_file in_file, out_file, shreds
File.open(out_file,"wb") do |file|
File.read(in_file).each_line.with_index do |line, index|
shreds.each do |shred|
if shred[:index]==index
line[shred[:begin]..shred[:end]]=shred[:replace]
end
end
file << line
end
end
end
shreds = [
{index:0, begin:0, end:4, replace:"11111"},
{index:1, begin:6, end:9, replace:"Line"}
]
replace_in_file in_file, out_file, shreds
=begin
=>contents of file out.txt
11111 This Is Me 12345 trying
23456 Line is line 2 987654
This is 345678 line 3 67890
=end

Resources