Related
This may seem simple.
It could be vbNewLine
or it can be
https://learn.microsoft.com/en-us/dotnet/api/system.environment.newline?redirectedfrom=MSDN&view=net-6.0#System_Environment_NewLine
However, that is NOT equivalent with "\n"
That is equivalent with
\r\n for non-Unix platforms, or \n for Unix platforms.
What about if I want \n no matter what. \
I tried to search for similar questions and I can't even find it.
There is nothing here either.
https://learn.microsoft.com/en-us/dotnet/api/system.environment.newline?redirectedfrom=MSDN&view=net-6.0#System_Environment_NewLine
So not easy to fine.
Update:
One answer says that "\n" means vbNewLine both in windows and in Linux.
Well, I am writing a vb.net windows program that interact with linux machine. You know, usual API stuff. In which case I need a character in windows that always mean "\n" in linux.
Basically, I need the chr(10) character. Not chr(10)+chr(13) character.
I think the answer I wrote my self is the answer to that.
And I do not think there is a simple answer on that.
Differences Between vbLf, vbCrLf & vbCr Constants may make things clear. However, people that find that question are people that already guess that vbLf may be a solution.
In fact, the questions and the answers over there do not even link "\n" to vbLF at all. They just say that vbLF is line feed. Is it "\n"? Another technicality
This question answer the question more directly. So what's equivalent to linux/unix "\n" no matter what is vbLf
using Microsoft.VisualBasic.CompilerServices;
namespace Microsoft.VisualBasic
{
// Summary:
// Represents a linefeed character for print and display functions.
[__DynamicallyInvokable]
public const string vbLf = "\n";
}
So the answer is
Microsoft.VisualBasic.vbLf
Somehow I can just use vbLf because Microsoft.VisualBasic is so often used it's in my project list I guess.
Update:
One answer says that "\n" means vbNewLine both in windows and in Linux.
Well, I am writing a vb.net windows program that interact with linux machine. You know, usual API stuff. In which case I need a character in windows that always mean "\n" in linux.
Basically, I need the chr(10) character. Not chr(10)+chr(13) character.
I think vbLf is the right answer.
And I do not think there is a simple answer on that.
Differences Between vbLf, vbCrLf & vbCr Constants may make things clear. However, people that find that question are people that already guess that vbLf may be a solution.
This question answer the question more directly. So what's equivalent to linux/unix "\n", which is the line feed chr(10) character no matter what is vbLf
For VB.Net, which tends to run on Windows, the closest map to \n is vbCrLf. This is different than just vbLf, because vbLf always maps directly to the ascii line feed character (10), but \n on some platforms will map to whatever the local system uses for line endings, rather than a simple line feed. On Windows, this is typically the 13/10 vbCrLf pair.
The easiest way to include these in code strings is via the new-ish interpolated strings:
$"This string{vbCrLf}includes some{vbCrLf}line breaks."
If you want to go platform-agnostic, the closest match is Environment.NewLine. And since that's a mouthful to use over and over you can always assing the value to a variable with a shorter name, like this:
Dim vbNl As String = Environment.NewLine
You ought to be using the ControlChars class in VB.NET. ControlChars.Lf is a line feed, i.e. equivalent to "\n", while ControlChars.Cr is a carriage return, i.e. equivalent to "\r". ControlChars.CrLf and ControlChars.NewLine are both equivalent to "\r\n". Environment.NewLine will give you "\r\n", "\n" or "\r", depending on the platform.
I have a scenario where I'm exporting tables to S3 using SELECT INTO S3... query.
This is the sample command(python) that I am using to export:
export_to_s3_sql = f"SELECT * FROM {database}.{table} " \
f"INTO S3 '{s3_full_path}/{database}/{table}/{table}' " \
f"CONFIG '{json.dumps(export_config)}' " \
f"CREDENTIALS '{json.dumps(export_creds)}' " \
f"FIELDS TERMINATED BY '\\t' ENCLOSED BY '\"' ESCAPED BY '\\\\' " \
f"LINES TERMINATED BY '\\r'"
Once export is complete, I read the tables using Spark. My tables are big in size(~2TB) and sometimes they contain newline characters in the column values.
Since they contain new line characters, I am forced to use multiline: true when reading the CSV via Spark. This causes Spark to read one file(~80GB) via one core with num partition equal to number of csv files.
My aim is to identify whether my CSV in S3 contains newline characters as column value or not when reading via Spark. So that I can remove multiline option from some of my spark jobs
One way to verify is to save the row counts during export. but a table might get updated during, before, or after the query execution.
My first question is, what will happen if the table gets updated during the execution of the above query?
During the export is there any simple way to know if any of the columns contain newline characters?
Can I somehow save row count in a separate file? Given that a table may get update instructions anytime.
During the export, Is there any way to replace all the newline characters in column values with empty strings?
Is there any way I can add a new column with some default value? I will use this flag to detect if my CSV has new lines characters as
Try adding a condition \\n with lines terminated by clause and also add FIELDS TERMINATED BY '\\t\\r\\n' so it can help skip empty lines and reduce extra space from the table.
export_to_s3_sql = f"SELECT * FROM {database}.{table} " \
f"INTO S3 '{s3_full_path}/{database}/{table}/{table}' " \
f"CONFIG '{json.dumps(export_config)}' " \
f"CREDENTIALS '{json.dumps(export_creds)}' " \
f"FIELDS TERMINATED BY '\\t\\r\\n' ENCLOSED BY '\"' ESCAPED BY '\\\\' " \
f"LINES TERMINATED BY '\\r\\n'"
As mentioned before in some questions with "Progress-4GL" and "OpenEdge" tags, I'm working with AppBuilder and Procedure editor. As a result, the debugging possibilities are extremely limited: for knowing the value of a variable, I need to do show them on screen, something like this:
MESSAGE "temp1=[" temp1 "], temp2=[" temp2 "]" VIEW-AS ALERT-BOX.
I can also put that information in a logfile, but that's not the main point here.
I would like to write a procedure, which can handle this, something like:
PROCEDURE SHOW_VARIABLES_AND_VALUES (INPUT I1, INPUT I2, ...):
1. <put parameter names and values together inside one string> => """I1="" I1"
2. <do this for all input parameters (the number is unknown)> => """I1="" I1, ""I2="" I2, ..."
3. <how to use this (MESSAGE VIEW-AS ALERT-BOX, LOG, ...) there I'll know what to do>
Does anybody know how to handle the fist two points (put variable name and value together and handle an unknown number of input parameters)?
Thanks in advance
You can use SUBSTITUTE function.
MESSAGE SUBSTITUTE ("temp1=&1 ~ntemp2=&2 ~n temp3=&3",
temp1,
temp2,
temp3) VIEW-AS ALERT-BOX.
Unfortunately there is no dynamic access to variables or parameters. So there's no way to automatically add all input parameters to a message string. Also there is no anytype parameter type in the ABL - for user defined functions or procedures. So you'd have to use the STRING() function a lot to convert your input parameters to string as the best fit parameter for everything.
The built in SUBSTITUTE function on the other hand can handle anytype of arguments. So temp1, temp2 and temp3 can actually be variables or parameters of any datatype.
As mentioned in one of my comments on one of your earlier questions: Give the OpenEdge debugger a chance. The debugger outside of Progress Developer studio looks historic. But it does it's job.
Meanwhile I've decided to use following system (as my request seems to be impossible):
MESSAGE "temp1=[" temp1 "]~n" ~
"temp2=[" temp2 "]~n" ~
"temp3=[" temp3 "]~n" ~
"temp4=[" temp4 "]" ~
VIEW-AS ALERT-BOX.
In order to make it easy to work with, I've found out the following keyboard "shortcut" for the tilde character: ALT+0126.
As indicated by Stefan, this is far better (no tilde and no shortcut needed):
MESSAGE "temp1=[" temp1 "]" SKIP
"temp2=[" temp2 "]" SKIP
"temp3=[" temp3 "]" SKIP
"temp4=[" temp4 "]" SKIP
VIEW-AS ALERT-BOX.
The following program prints out the name of the file, the number of rows, and the number of rows that begin with // in the case that more than one fifth of the rows begin that way.
awk '$1 == "//" { a+=1 } END { if (a * 5 >= NR) {print FILENAME " " NR " " a}}' MyClass.java
This works, but the nested {{}} make me question if I'm doing it right, knowing that the typical structure of an awk program is:
awk 'condition { actions }'
So I suspect that something like
awk '$1 == "//" { a+=1 } END && (a * 5 >= NR) {print FILENAME " " NR " " a}' MyClass.java
would be more appropriate, but every such attempt gives syntax errors. Is there a right way to do this, or is my approach as good as it gets.
There are other ways to express it, but you wrote it idiomatically the first time. Although the authors tend to omit braces whenever they can, you can still find examples of code like that throughout The AWK Programming Language. They should know.
It seems like Aho, Weinberger, and Kernighan have several centuries of development experience in languages whose syntax derives from C. And when they write something like this
if (a * 5 >= NR)
print FILENAME " " NR " " a
it communicates perfectly that the block following the if statement is supposed to contain one and only one statement.
I have considerably fewer centuries of experience. Whenever I read something like that, it communicates perfectly that a) somebody forgot to type {}, and b) somebody else is about to introduce a bug by adding a statement to that block without adding the braces.
Over the years, I've trained myself to type this whenever I type an if.
if () {}
Then I go back and fill it in, breaking lines if I need to. In my normal editor, "if" expands automatically to "if () {}". I'm pretty sure I haven't omitted braces even once since the mid-1980s.
I frequently come across this problem. I have a file:
something
something2
something3
which I want output as:
"something","something2","something3"
any quick tool for this, preferably online?
If its just a one off thing, it'd be pretty easy to just do it with a search & replace in any advanced-ish text editor...
For example in notepad++:
Do a Replace (CTRL+H)
Set "Search Mode" to "Extended"
Find: \r\n
Replace with: ","
(of course you'll need an extra quote at the very start & very end of the file).
If you need to do it more than once, writing a small script/program that did a regular expression replace over the file would be fairly straight forward too.
Edit: If you really wanted to do it online, you could use an online regular expression tester (in this case you want to use \n as the regex and "," as your replace pattern, leaving the other settings alone).
A quick Python hack?
lines = open('input.txt').xreadlines()
txt = ','.join(['"%s"' % x for x in lines])
open('output.txt', 'w').write(txt)