Delete empty line if there is only one between two lines, if there are two empty lines remove only one
Hi Team I am getting db2 output as of below.
this is testing 1
this is testing 2
this is testing 4
The db2 provides output as, output for a query and a empty row and if there is no data for the query it provides one empty row
I want to remove the additional empty line after each output.
I know sed -i '/^$/d' file.txt
can remove empty rows.
Is there a way to remove single empty row after each row which has data.
This is the desired output:
this is testing 1
this is testing 2
this is testing 4
shell-script
New contributor
add a comment |
Hi Team I am getting db2 output as of below.
this is testing 1
this is testing 2
this is testing 4
The db2 provides output as, output for a query and a empty row and if there is no data for the query it provides one empty row
I want to remove the additional empty line after each output.
I know sed -i '/^$/d' file.txt
can remove empty rows.
Is there a way to remove single empty row after each row which has data.
This is the desired output:
this is testing 1
this is testing 2
this is testing 4
shell-script
New contributor
add a comment |
Hi Team I am getting db2 output as of below.
this is testing 1
this is testing 2
this is testing 4
The db2 provides output as, output for a query and a empty row and if there is no data for the query it provides one empty row
I want to remove the additional empty line after each output.
I know sed -i '/^$/d' file.txt
can remove empty rows.
Is there a way to remove single empty row after each row which has data.
This is the desired output:
this is testing 1
this is testing 2
this is testing 4
shell-script
New contributor
Hi Team I am getting db2 output as of below.
this is testing 1
this is testing 2
this is testing 4
The db2 provides output as, output for a query and a empty row and if there is no data for the query it provides one empty row
I want to remove the additional empty line after each output.
I know sed -i '/^$/d' file.txt
can remove empty rows.
Is there a way to remove single empty row after each row which has data.
This is the desired output:
this is testing 1
this is testing 2
this is testing 4
shell-script
shell-script
New contributor
New contributor
edited 18 hours ago
cryptarch
5116
5116
New contributor
asked 19 hours ago
Anthony
61
61
New contributor
New contributor
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
This will only remove a newline if after some character (only the first newline after a line with text):
sed -n '$!N;s/(.)n/1/;P;D' file
add a comment |
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 4
This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.
This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):
$ cat file
this is testing 1
this is testing 2
this is testing 5
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 5
add a comment |
awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt
Explanation
BEGIN {prev_empty = "TRUE"}
: start by initialising the variableprev_empty
toTRUE
.
if (/^$/)
: check if the line is empty (i.e. matches the regex/^$/
).- If so, then
{if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}
: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variableprev_empty
toTRUE
. - If the current line is not empty,
{prev_empty = ""; print}
: set the variableprev_empty
to nothing, and print the line.
The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/
), and the previous line was non-empty (i.e. prev_empty = ""
).
add a comment |
If the file is small enough to fit in memory, you can slurp the file with perl
and remove any newline that comes after a non-space character and another newline:
$ perl -0pe 's/(Sn)n/$1/g' file
this is testing 1
this is testing 2
this is testing 4
add a comment |
With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.
With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk
) you can prevent that by setting an empty output record separator if the actual input record separator is empty:
$ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
this is testing 1
this is testing 2
this is testing 4
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Anthony is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492410%2fdelete-empty-line-if-there-is-only-one-between-two-lines-if-there-are-two-empty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
This will only remove a newline if after some character (only the first newline after a line with text):
sed -n '$!N;s/(.)n/1/;P;D' file
add a comment |
This will only remove a newline if after some character (only the first newline after a line with text):
sed -n '$!N;s/(.)n/1/;P;D' file
add a comment |
This will only remove a newline if after some character (only the first newline after a line with text):
sed -n '$!N;s/(.)n/1/;P;D' file
This will only remove a newline if after some character (only the first newline after a line with text):
sed -n '$!N;s/(.)n/1/;P;D' file
answered 18 hours ago
Isaac
11.4k11650
11.4k11650
add a comment |
add a comment |
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 4
This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.
This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):
$ cat file
this is testing 1
this is testing 2
this is testing 5
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 5
add a comment |
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 4
This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.
This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):
$ cat file
this is testing 1
this is testing 2
this is testing 5
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 5
add a comment |
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 4
This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.
This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):
$ cat file
this is testing 1
this is testing 2
this is testing 5
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 5
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 4
This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.
This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):
$ cat file
this is testing 1
this is testing 2
this is testing 5
$ awk '{ print; if (length) getline }' file
this is testing 1
this is testing 2
this is testing 5
edited 17 hours ago
answered 18 hours ago
Kusalananda
122k16230375
122k16230375
add a comment |
add a comment |
awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt
Explanation
BEGIN {prev_empty = "TRUE"}
: start by initialising the variableprev_empty
toTRUE
.
if (/^$/)
: check if the line is empty (i.e. matches the regex/^$/
).- If so, then
{if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}
: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variableprev_empty
toTRUE
. - If the current line is not empty,
{prev_empty = ""; print}
: set the variableprev_empty
to nothing, and print the line.
The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/
), and the previous line was non-empty (i.e. prev_empty = ""
).
add a comment |
awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt
Explanation
BEGIN {prev_empty = "TRUE"}
: start by initialising the variableprev_empty
toTRUE
.
if (/^$/)
: check if the line is empty (i.e. matches the regex/^$/
).- If so, then
{if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}
: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variableprev_empty
toTRUE
. - If the current line is not empty,
{prev_empty = ""; print}
: set the variableprev_empty
to nothing, and print the line.
The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/
), and the previous line was non-empty (i.e. prev_empty = ""
).
add a comment |
awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt
Explanation
BEGIN {prev_empty = "TRUE"}
: start by initialising the variableprev_empty
toTRUE
.
if (/^$/)
: check if the line is empty (i.e. matches the regex/^$/
).- If so, then
{if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}
: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variableprev_empty
toTRUE
. - If the current line is not empty,
{prev_empty = ""; print}
: set the variableprev_empty
to nothing, and print the line.
The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/
), and the previous line was non-empty (i.e. prev_empty = ""
).
awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt
Explanation
BEGIN {prev_empty = "TRUE"}
: start by initialising the variableprev_empty
toTRUE
.
if (/^$/)
: check if the line is empty (i.e. matches the regex/^$/
).- If so, then
{if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}
: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variableprev_empty
toTRUE
. - If the current line is not empty,
{prev_empty = ""; print}
: set the variableprev_empty
to nothing, and print the line.
The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/
), and the previous line was non-empty (i.e. prev_empty = ""
).
edited 18 hours ago
answered 19 hours ago
Sparhawk
9,33263991
9,33263991
add a comment |
add a comment |
If the file is small enough to fit in memory, you can slurp the file with perl
and remove any newline that comes after a non-space character and another newline:
$ perl -0pe 's/(Sn)n/$1/g' file
this is testing 1
this is testing 2
this is testing 4
add a comment |
If the file is small enough to fit in memory, you can slurp the file with perl
and remove any newline that comes after a non-space character and another newline:
$ perl -0pe 's/(Sn)n/$1/g' file
this is testing 1
this is testing 2
this is testing 4
add a comment |
If the file is small enough to fit in memory, you can slurp the file with perl
and remove any newline that comes after a non-space character and another newline:
$ perl -0pe 's/(Sn)n/$1/g' file
this is testing 1
this is testing 2
this is testing 4
If the file is small enough to fit in memory, you can slurp the file with perl
and remove any newline that comes after a non-space character and another newline:
$ perl -0pe 's/(Sn)n/$1/g' file
this is testing 1
this is testing 2
this is testing 4
answered 17 hours ago
terdon♦
128k31249424
128k31249424
add a comment |
add a comment |
With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.
With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk
) you can prevent that by setting an empty output record separator if the actual input record separator is empty:
$ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
this is testing 1
this is testing 2
this is testing 4
add a comment |
With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.
With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk
) you can prevent that by setting an empty output record separator if the actual input record separator is empty:
$ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
this is testing 1
this is testing 2
this is testing 4
add a comment |
With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.
With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk
) you can prevent that by setting an empty output record separator if the actual input record separator is empty:
$ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
this is testing 1
this is testing 2
this is testing 4
With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.
With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk
) you can prevent that by setting an empty output record separator if the actual input record separator is empty:
$ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
this is testing 1
this is testing 2
this is testing 4
answered 13 hours ago
steeldriver
34.5k35083
34.5k35083
add a comment |
add a comment |
Anthony is a new contributor. Be nice, and check out our Code of Conduct.
Anthony is a new contributor. Be nice, and check out our Code of Conduct.
Anthony is a new contributor. Be nice, and check out our Code of Conduct.
Anthony is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492410%2fdelete-empty-line-if-there-is-only-one-between-two-lines-if-there-are-two-empty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown