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

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.

Related

How to delete from a text file, all lines that DO NOT contain a specific string? [duplicate]

This question already has answers here:
sed delete lines not containing specific string
(4 answers)
Remove all lines except matching pattern line best practice (sed)
(4 answers)
Closed 17 days ago.
How would I use sed to delete all lines in a text file which do not contain a specific string?
sed '/pattern to match/d' ./infile
I used this command, but it deletes the lines MATCHING a specific pattern,
but what I want is to delete the lines which DO NOT contain a specific pttern?

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'?

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

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. :-)

Import from file having line breaks [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
How to import data from a export file having the line breaks in a column.
for example i have a table with column as remarks, when the user entered the remarks from frontend of application it allowed the line breaks and the same data is stored in the table. but when export is taken and tried to import to other table the import failed due to line breaks.
Assuming you're using the IMPORT or LOAD utilities to load data into DB2 for Linux, UNIX and Windows:
If your character fields are properly quoted (i.e. they have " at the beginning and end of each character field in the file), then you can use the DELPRIORITYCHAR file-type modifier:
import from yourfile.csv
of del modified by delprioritychar
insert into yourtable
This instructs the IMPORT and LOAD utilities to prioritize the character delimiter (") over the record delimiter (i.e. newline).
I think the easiest approach would be to replace the line break characters with something else, e.g. the escape sequences \n, then, if needed, replace them back after importing.

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.