append previous line with current line if the file contains blanks [closed] - perl

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to append lines to the previous line if the blank line is followed by the line.
For example:
A
B
C
1
D
E
B
Ouput:
A
B
C
1D
EB

See also Håkon Hægland's comment above. This is one of those problems that lends itself to treating the whole file as one long string (with embedded newline characters). Perl has a flag -0 for changing or turning off the default "record separator" definition that accomplishes this. Then you just need to realize that, depending on your definition of "blank line", you seem to be requesting that all sequences of two or more newline characters in a row simply be removed. (If your definition of "blank line" can include lines with blank characters on them (whitespace, i.e. spaces and tabs), you'll need a more complicated expression.) This compact one-liner will do it:
$ perl -0pe 's/\n\n+//g' blanklines
A
B
C
1D
EB
Now please tell me this was not a homework assignment.
Update: I realized a couple of additional things. 1) Since newline is included in Perl's whitespace special escape \s, expanding to handle the case of blank lines having blank characters on them is not really more complex: perl -0pe 's/\n\s+//g' blanklines. 2) There is an edge case that this solution doesn't handle right: blank lines at the end of the input. I'll leave that as a problem for the student. :-)

Related

Replace multiple lines of whitespace with one line using sed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
How could I use sed to replace multiple lines of whitespace (including tabs and spaces) with just one empty line?
Example input:
Line 1
<Space><Space>
Line 2
<Tab>
Line 3
Line 4
Desired output:
Line 1
Line 2
Line 3
Line 4
This might work for you (GNU sed):
sed 'N;s/^[[:blank:]]*$//mg;/^\n$/!P;D' file
Append the next line.
Remove any spaces or tabs from blank lines.
If the first line of the possible two is empty do not print.
Delete upto and including the first newline or if a newline is not present, delete the line.
N.B. The P and D commands are special.

Replace letters in a word with a character [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I need to replace certain letters in a word with a character.
I'm stuck. How can I fix it?
It looks like you already have most of the code. You just need to add the letters that don't need to be replaced and are not in the consonants list.
To do this, I would use an if-else instead of an if in your loop. This will allow you to replace letters that are in the consonants list and add letters that don't need to be replaced to the final word.
It would look something like this:
Note that the consonants list has the characters that are to be replaced in word word.
This should give you what you are looking for.

Rust Command Line Arguments [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I'm experimenting with command line arguments in Rust.
Windows Power Shell.
if let Some(a) = env::args().nth(1) {
println!("parâmetro é {:?}", &a);
} else ...
ARG = "Qu'il" prints "parâmetro é Qu\'il" (ok)
ARG = Qu'il (results in apparent infinite loop)
ARG = Qu\il (idem)
So, what's the proper way to handle this (the error)?
Thanks in advance.
I can't really discern what error you are referring to, so I'll explain the reasoning for the behavior you are seeing:
You are using the format specifier {:?} which makes Rust print the string in a debug-friendly manner. If you use println!("parâmetro é {}", &a); instead you'd see parâmetro é Qu'il
This is not an infinite loop. Apostrophes (') are special characters in PowerShell to define a literal string. The reason why nothing is happening is that PowerShell is waiting for you to write the rest of the string and finish with another apostrophe.
Like nr. 1
See more here:
About literal strings in PowerShell
Can I use a single quote in a PowerShell 'string'?

E PROBLEMS when using Excel [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
Hi my problem is everytime I use excel and place an E in the cell with numbers it turns it into a mathematical equation.
Example: 6807E02 turns to 6.81E+05. Totally driving me insane. Someone please help me?
This is happening because Excel assumes you are entering a number using a variant of scientific notation called E Notation. When you enter 6807E02, Excel assumes you are talking about the number 680,700 (6807 x 10 ^ 2). Excel has it's own method of formatting scientific notation, which is to always put the decimal to the right of the first digit, and end with the E (the multiple of 10) to the far right. That's why you're seeing 6.81E+05 (the display rounds to two digits after the decimal, but the number is still the same).
If you're intentionally trying to type in the text value "6807E02", you can either set the cell's data type to "Text" (Image below), or you can type the single apostrophe key (') before you start entering the number. If you want the number you typed in, but you don't want Excel to override your number formatting, that's unfortunately beyond my abilities. Excel is pretty stringent with custom formatting when it comes to Scientific Notation. You would probably end up spending far more time trying to force Excel to accept your notation as a custom format than the value doing so would add to your project.
Hope this helps!
Hi this link should help you,
From what I can see the easiest solution would be if you put an apostrophe (') in front of the number excel will treat it as text.
For example '6807E02

Write a limited number of characters then return to the line in Perl and do not cut the last word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a character string in an array and I want to write it to a file. The problem is I need to write for a number of characters limited to 100 then I must return to the the line and finish the content of the sentence. Then if I happen to 100 characters again I return to the line until I finish my chain content in the table.
The Text::Wrap module is likely to do what you want, and is a part of core Perl meaning you don't have to install it.
This program reads the text from the DATA pseudo-handle and reformats it.
use strict;
use warnings;
use Text::Wrap;
my #text = <DATA>;
chomp #text;
$Text::Wrap::columns = 100;
print Text::Wrap::wrap '', '', #text;
__DATA__
"Text::Wrap::wrap()" is a very simple paragraph formatter. It formats a
single paragraph at a time by breaking lines at word boundaries.
Indentation is controlled for the first line ($initial_tab) and all
subsequent lines ($subsequent_tab) independently. Please note:
$initial_tab and $subsequent_tab are the literal strings that will be
used: it is unlikely you would want to pass in a number.
Text::Wrap::fill() is a simple multi-paragraph formatter. It formats
each paragraph separately and then joins them together when it's done.
It will destroy any whitespace in the original text. It breaks text into
paragraphs by looking for whitespace after a newline. In other respects
it acts like wrap().
Both "wrap()" and "fill()" return a single string.
output
"Text::Wrap::wrap()" is a very simple paragraph formatter. It formats a single paragraph at a time
by breaking lines at word boundaries. Indentation is controlled for the first line ($initial_tab)
and all subsequent lines ($subsequent_tab) independently. Please note: $initial_tab and
$subsequent_tab are the literal strings that will be used: it is unlikely you would want to pass in
a number. Text::Wrap::fill() is a simple multi-paragraph formatter. It formats each paragraph
separately and then joins them together when it's done. It will destroy any whitespace in the
original text. It breaks text into paragraphs by looking for whitespace after a newline. In other
respects it acts like wrap(). Both "wrap()" and "fill()" return a single string.
I think you're looking for something like Text::Reform or Text::Autoformat.