Remove trailing hash'#' in tab delimited file-SAP ABAP - abap

I am reading a file from app server. It is a tab delimited file , I am trying to remove trailing hash(#) , But I am unable to do so.
I am using split wa_string at cl_abap_char_utilities=>horizontal_tab .
Even CA Contains Any does not seem to work.
LOOP AT ifile.
CLEAR: wa_string.
wa_string = ifile.
SPLIT wa_string AT cl_abap_char_utilities=>horizontal_tab
INTO gt_load-werks
gt_load-edatv
gt_load-matnr
gt_load-qty.
SHIFT gt_load-qty RIGHT DELETING TRAILING l_tab.
IF gt_load-qty CA '#'.
lv_len = strlen( gt_load-qty ).
IF lv_len < 8 .
lv_len1 = lv_len - 1.
gt_load-qty = gt_load-qty+0(lv_len1).
CONDENSE gt_load-qty.
ENDIF.
ENDIF.
ENDLOOP.

It is not '#' really. I think it is just placeholder for symbol with code 10 (0x0A) or 13 (0x0D). Look at qt_load-qty in hex mode in debugger.

Related

How to escape double quotes within a data when it is already enclosed by double quotes

I have CSV data separated by comma like below which has to be imported into snowflake table using copy command .
"1","2","3","2"In stick"
Since I am already passing the parameter OPTIONALLY_ENCLOSED_BY = '"' to copy command I couldn't escape the " (double quotes) within the data ("2"In stick") .
The imported data that I want to see in the table is like below
1,2,3,2"In stick
Can someone please help here ? Thanks !
If you are on Windows, I have a funny solution for that. Open this CSV file in MS Excel. Excel consumes correct double quotes to show data in the cellular format and leaves the extra in the middle of a cell (if each cell is separated properly by commas). Then choose 'replace' and replace double quotes with something else (like two single quotes or replace by nothing to remove them). Then save it again as a CSV. I assume other spreadsheet programs should do the same.
If you have an un-escaped quote inside a field which is surrounded by quotes that isn't really valid CSV. For example, here is an excerpt from the RFC4180 spec
If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with another double quote.
For example:
"aaa","b""bb","ccc"
I think that whatever is generating the CSV file is doing it incorrectly and needs to be fixed before you will be able to load it into Snowflake. I don't think any file_format option will be able to solve this for you since it's not valid CSV.
The CSV row should either look like this:
"1","2","3","2""In stick"
or this:
"1","2","3","2\"In stick"
I had this same problem, and while writing up the question, I found an answer:
Import RFC4180 files (CSV spec) into snowflake? (Unable to create file format that matches CSV RFC spec)
Essentially, set:
Name
Value
Column Separator
Comma
Row Separator
New Line
Header lines to skip
{you have to decide what to put here}
Field optionally enclosed by
Double Quote
Escape Character
None
Escape Unenclosed Field
None
Here is my ALTER statement:
ALTER FILE FORMAT "DB_NAME"."SCHEMA_NAME"."CSV_SPEC3" SET COMPRESSION = 'NONE' FIELD_DELIMITER = ',' RECORD_DELIMITER = '\n' SKIP_HEADER = 1 FIELD_OPTIONALLY_ENCLOSED_BY = '\042' TRIM_SPACE = FALSE ERROR_ON_COLUMN_COUNT_MISMATCH = TRUE ESCAPE = 'NONE' ESCAPE_UNENCLOSED_FIELD = 'NONE' DATE_FORMAT = 'AUTO' TIMESTAMP_FORMAT = 'AUTO' NULL_IF = ('\\N');
As I mention in the answer, I don't know why the above works, but it is working for me. Go figure.

How to add asterisk to a list of filenames and then make it a line using Notepad++

I have a list of file names (about 4000).
For example:
A-67569
H-67985
J-87657
K-85897
...
I need to put an asterisk before and after each file name. And then make it a line format.
Example:
*A-67569* *H-67985* *J-87657* *K-85897* so on...
Note that there is a space between filenames.
Forgot to mention, I'm trying to do this with Notepad++
How can I do it?
Please advise.
Thanks
C# example for list to string plus edits
List<string> list = new List<string> { "A - 67569"), "H-67985", "J-87657", "K-85897"};
string outString = "";
foreach(string item in list)
{
outString += "*" + item + "* ";
}
content of outstring: *A - 67569* *H-67985* *J-87657* *K-85897*
Use the Replace of your Notedad++ (Search > Replace..)
Select Extended (\n \r \t \0 \x...) on the bottom of the Replace window
In the field Find what write '\r\n' and in the field Replace with write * *
Replace all
Note, that you should manually place the single asterisk before the first and after the last words.
If this won't work, in step 2. instead of \r\n try to use only \n or \r.
You can use Regular expression in the search Mode.
Find what:
(\S+)(\R|$)
Replace with:
*$1
Note the space after de number one
For the archive
A-67569
H-67985
J-87657
K-85897
Output:
*A-67569 *H-67985 *J-87657 *K-85897
Explication of regex:
(\S+) Mean find one or more caracters is not a blank.
(\R|$) Mean find any end of line or end of file
(\S+)(\R|$) Mean find any gorup of caracters not blank ho end with end of line or end of file.
Explication of Replace with
When you use the $ simpbol, you are using a reference to the groups finded, $1 is the first group, in this case the group (\S+).

AutoIt ControlSend() function occasionally replaces hyphens with underscores

I'm using an AutoIt script to automate interaction with a GUI, and part of the process involves using the ControlSend() function to place a file path into a combo box. The majority of the time, the process works properly, but occasionally ( ~ 1/50 calls to the function? ) a single hyphen in the filepath is replaced with an underscore. The script is to be run unsupervised for bulk data processing, and such an error typically results in a forced-focus popup that screams "The file could not be found!" and halts further processing.
Unfortunately, due to the character limit of the combo box, I cannot supply all 16 arguments with a single call, and I am forced to load each of the images individually using the following for-loop:
;Iterate through each command line argument (file path)
For $i = 1 To $CmdLine[0]
;click the "Disk" Button to load an image from disk
ControlClick("Assemble HDR Image", "", "[CLASS:Button; TEXT:Disk; Instance:1]")
;Give the dialogue time to open before entering text
Sleep(1000)
;Send a single file path to the combo box
ControlSend("Open", "" , "Edit1", $CmdLine[$i])
;"Press Enter" to load the image
Send("{ENTER}")
Next
In an errant run, the file path
C:\my\file\path\hdr_2016-04-22T080033_00_rgb
^Hyphen
is converted to
C:\my\file\path\hdr_2016_04-22T080033_00_rgb
^Underscore
Due to the existence of both hyphens and underscores in the file name, it is difficult to perform a programmatic correction (e.g. replace all underscores with hyphens).
What can be done to correct or prevent such an error?
This is both my first attempt at GUI automation and my first question on SO, and I apologize for my lack of experience, poor wording, or deviations from StackOverflow convention.
Just use ControlSetText instead of ControlSend as it will set the complete Text at once and won't allow other keystrokes (like Shift) to interfere with the many virtual keystrokes that the Send-function fires.
If the hyphen is the problem and you need to replace it, you can do so:
#include <File.au3>
; your path
$sPath = 'C:\my\file\path'
; get all files from this path
$aFiles = _FileListToArray($sPath, '*', 1)
; if all your files looks like that (with or without hyphen), you can work with "StringRegExpReplace"
; 'hdr_2016-04-22T080033_00_rgb'
$sPattern = '(\D+\d{4})(.)(.+)'
; it means:
; 1st group: (\D+\d{4})
; \D+ one or more non-digit, i.e. "hdr_"
; \d{4} digit 4-times, i.e. "2016"
; 2nd group: (.)
; . any character, hyphen, underscore or other, only one character, i.e. "~"
; 3rd group: (.+)
; . any character, one or more times, i.e. "22T080033_00_rgb"
; now you change the filename for all cases, where this pattern matches
Local $sTmpName
For $i = 1 To $aFiles[0]
; check for pattern match
If StringRegExp($aFiles[$i]) Then
; replace the 2nd group with underscore
$sTmpName = StringRegExpReplace($aFiles[$i], $sPattern, '\1_\3')
FileMove($sPath & '\' & $aFiles[$i], $sPath & '\' & $sTmpName)
EndIf
Next

Write multiple lines to text file with '\n'

I have a program that iterates over all lines of a text file, adds spaces between the characters, and writes the output to the same file. However, if there are multiple lines in the input, I want the output to have separate lines as well. I tried:
let text = format!(r"{}\n", line); // Add newline character to each line (while iterating)
file.write_all(text.as_bytes()); // Write each line + newline
Here is an example input text file:
foo
bar
baz
And its output:
f o o\n b a r\n b a z
It seems that Rust treats "\n" as an escaped n character, but using r"\n" treats it as a string. How can I have Rust treat \n as a newline character to write multiple lines to a text file?
Note: I can include the rest of my code if you need it, let me know.
Edit: I am on Windows 7 64 bit
The problem is the 'r' in front of your string. Remove it and your program will print newlines instead of '\n'.
Also note that only most Unices use '\n' as newline. Windows uses "\r\n".

inserting character in file , jython

I have written a simple program where to read the first 4 characters and get the integer of it and read those many character and write xxxx after it . Although the program is working the only issues instead of inserting the character , its replacing.
file = open('C:/40_60.txt','r+')
i=0
while 1:
char = int(file.read(4))
if not char: break
print file.read(char)
file.write('xxxx')
print 'done'
file.close()
I am having issue with writing data .
considering this is my sample data
00146456135451354500107589030015001555854640020
and expected output is
001464561354513545xxxx00107589030015001555854640020
but actually my above program is giving me this output
001464561354513545xxxx7589030015001555854640020
ie. xxxx overwrites 0010.
Please suggest.
Files do not support an "insert"-operation. To get the effect you want, you need to rewrite the whole file. In your case, open a new file for writing; output everything you read and in addition, output your 'xxxx'.