I have the following simple line to print a new line to a log file:
Print #fileNumber, vbNewLine
However, this results in 2 newlines instead of one. My code does not have any other vbNewLines or anything that would print newlines.
If I do not have this print line, then I print no newlines, so this means this line is printing 2 newlines.
Does anyone have any ideas why?
Just tested this, and Print always adds a linebreak.
So simply using Print #fileNumber, will result in 1 blank line.
The problem is that Print already prints on a new line, so when you add vbnewline you're getting 2 lines.
Related
This exercise is from the AWK one-liners explained blog post by Peteris Krumins
Essentially this line
awk '/\\$/ { sub(/\\$/,""); getline t; print $0 t; next }; 1'
joins every line ending with backslash with the next line:
e.g. input
12345\
6789
523435\
00000
Output
123456789
52343500000
The blog post says:
Unfortunately this one liner fails to join more than 2 lines (this is left as an exercise to the reader to come up with a one-liner that joins arbitrary number of lines that end with backslash :)).
So using the AWK one-liner above, and if you use an input file with 2 or more lines one after the other that has a backslash at the end (input2), gives an incorrect answer (output2)
e.g. input2
12345\
6789\
523435\
00000
Output 2 - INCORRECT
123456789\
52343500000
I think, according to the post, the output should instead be output3:
Output 3 - CORRECT
12345678952343500000
How can one solve this problem (input as input2 and getting output3)?
Try the following:
awk '/\\$/ { printf "%s", substr($0, 1, length($0)-1); next } 1' <<'EOF'
12345\
6789\
523435\
00000
EOF
which yields
12345678952343500000
This demonstrates that 3 consecutive (or more) line continuations work fine, unlike with the command in the question.
Explanation of the command:
/\\$/ matches a \ at the end ($) of a line, signaling line continuation.
substr($0, 1, length($0)-1) removes that trailing \ from the input line, $0.
By using printf "%s", the (modified) current line is printed without a trailing newline, which means that whatever print command comes next will directly append to it, effectively joining the current and the next line.
next finishes processing of the current line.
1 is a common awk idiom that is shorthand for { print }, i.e., for simply printing the input line (with a trailing \n).
As for why the original command doesn't work:
awk '/\\$/ { sub(/\\$/,""); getline t; print $0 t; next }; 1
On encountering a line-continuation character (\ at the end of the current line), getline t reads the next line from the file and prints it as is after the current line.
next then finishes processing of both the current and - thanks to the getline call - the next line, so that the next script cycle processes the line after the next line (2 lines from the current one).
Therefore, since the line read via getline is blindly printed and not examined in any way, it is skipped with respect to line-continuation-character processing.
In general, as Ed Morton points out in a comment, use of getline is rarely the right solution and can lead to subtle bugs - see http://awk.info/?tip/getline.
I have a textfile that contains a story with many paragraphs and I want to print the story in the terminal, I also want to make it so when it finds a blank line in the textfile it adds another blank line to this in the whole story and print this as a new file.
I have this code but it only appends a new line when it finds a fullstop (so it adds a blank line to every sentence) and prints this as a new file:
awk -v RS="." '/^./ { print " " $0 " " }' < 52293-0.txt > output
What mistake am I making?
Many thanks,
I am using Pycharm and trying to print text to the console
print('>')
>
print('>>')
>>
print('>>>')
The final print command ('>>>') prints a blank line. Why is that? How can I print out the three >'s?
If you can tolerate the space, you can do:
print(" >>>")
This may be naive: how do I print a blank line from a gnuplot script to a txt file? I mean without any spaces, just a return.
set print "filename" append # I print things in a loop and append them
print a
print b
print HowToPrintBlankLines?
Thanks,
Luca
To print a blank line in gnuplot you can just do
print ""
I have an assignment asking me to print x iterations of a string for each character in that string. So if the string input is "Gum", then it should print out:
Gum
Gum
Gum
Right now my code is
my $string = <>;
my $length = length($string);
print ($string x $length, "\n");
And I'm getting gum printed five times as my output.
Those who have said you will get CR + LF at the end of the line on a Windows system are mistaken. Perl will convert the native line ending to a simple newline \n on any platform.
You must bear this in mind whether you are reading from the terminal or from a file.
The built-in chomp function will remove the line terminator character from the end of a string variable. If the string doesn't end with a line terminator then it will have no effect.
So when you type GumEnter you are setting $string to "Gum\n", and length will show that it has four characters.
You are seeing it five times on your screen because the first line is what you typed in yourself. The following four are printed by the program.
After a chomp, $string is just "Gum" with a length of three characters, which is what you want.
To output this on separate lines you have to print a newline after each line, so you can write
my $string = <>;
chomp $string;
my $length = length $string;
print ("$string\n" x $length);
or perhaps
print $string, "\n" for 1 .. $length;
I hope that helps
As you are simply using the input string, it still contains the newline at the end. This is also counted as a character. On my system, it outputs 4 Gum\n.
chomp($string) will remove the line ending, but the output will then also run together, resulting in GumGumGum\n
When You insert input and press enter afterwards You don't enter "Gum" but "Gum\r\n" which is a string of length 5. You should do trimming.
Your code is working fine. See this: http://ideone.com/AsPFh3
Possibility 1: It might be that you're putting 2 spaces while giving input from command line, that's why the length comes out to be 5, and it prints 5 times. Something like this: http://ideone.com/fsvnrd
In above case the my $string=<>; will give you my $string = "gum "; so length will be 5.
Possibility 2:
Another possibility is that if you use Windows then it will add carriage return (\r) and new line (due to enter \n) at the end of string. So it makes the length 5.
Edit: To print in new line: Use the below code.
#!/usr/bin/perl
# your code goes here
chomp(my $string=<>);
my $length = length($string);
print ("$string\n" x $length);
Demo
Edit 2: To remove \r\n use the below:
$string=~ s/\r|\n//g; Read more here.