How to remove empty/blank lines in a file in Unix? The Next CEO of Stack OverflowUseful...
How a 64-bit process virtual address space is divided in Linux?
Is wanting to ask what to write an indication that you need to change your story?
is it ok to reduce charging current for li ion 18650 battery?
Does increasing your ability score affect your main stat?
Powershell. How to parse gci Name?
How to install OpenCV on Raspbian Stretch?
Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis
Make solar eclipses exceedingly rare, but still have new moons
Would be okay to drive on this tire?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
Is there always a complete, orthogonal set of unitary matrices?
What did we know about the Kessel run before the prequels?
Why the difference in type-inference over the as-pattern in two similar function definitions?
No sign flipping while figuring out the emf of voltaic cell?
What steps are necessary to read a Modern SSD in Medieval Europe?
Rotate a column
How to scale a tikZ image which is within a figure environment
Why is information "lost" when it got into a black hole?
Can a Bladesinger Wizard use Bladesong with a Hand Crossbow?
Need help understanding a power circuit (caps and diodes)
Solving system of ODEs with extra parameter
Chain wire methods together in Lightning Web Components
Is a distribution that is normal, but highly skewed considered Gaussian?
Find non-case sensitive string in a mixed list of elements?
How to remove empty/blank lines in a file in Unix?
The Next CEO of Stack OverflowUseful Command-line Commands on WindowsHow to convert line breaks in a text file between the Windows and Unix/Linux formats?How do I find the total number of lines in a set of found files (using Linux command-line tools)?Command to delete specific lines after searching for a phrase from a fileCommand to insert/prefix characters in lines after searching for a phrase from a fileSome copied files have additional blank linesUnwanted blank lines when committing from SVNperl + sed + remove lines that start with “#”How to display the first N lines of a command output in Windows? (the equivalent of Unix command “head”)how to use awk or sed command (OS level commands) in Sybase level
How do I remove empty/blank (including spaces only) lines in a file in Unix/Linux using the command line?
contents of file.txt
Line:Text
1:<blank>
2:AAA
3:<blank>
4:BBB
5:<blank>
6:<space><space><space>CCC
7:<space><space>
8:DDD
output desired
1:AAA
2:BBB
3:<space><space><space>CCC
4:DDD
linux unix command-line-interface text parsing
add a comment |
How do I remove empty/blank (including spaces only) lines in a file in Unix/Linux using the command line?
contents of file.txt
Line:Text
1:<blank>
2:AAA
3:<blank>
4:BBB
5:<blank>
6:<space><space><space>CCC
7:<space><space>
8:DDD
output desired
1:AAA
2:BBB
3:<space><space><space>CCC
4:DDD
linux unix command-line-interface text parsing
1
For awk, see: Remove blank lines in awk, or using grep, in general, see: How to remove blank lines from a file in shell?
– kenorb
May 5 '15 at 16:11
same question and same answer given over here but with much more votes
– Trevor Boyd Smith
Feb 21 '18 at 19:18
add a comment |
How do I remove empty/blank (including spaces only) lines in a file in Unix/Linux using the command line?
contents of file.txt
Line:Text
1:<blank>
2:AAA
3:<blank>
4:BBB
5:<blank>
6:<space><space><space>CCC
7:<space><space>
8:DDD
output desired
1:AAA
2:BBB
3:<space><space><space>CCC
4:DDD
linux unix command-line-interface text parsing
How do I remove empty/blank (including spaces only) lines in a file in Unix/Linux using the command line?
contents of file.txt
Line:Text
1:<blank>
2:AAA
3:<blank>
4:BBB
5:<blank>
6:<space><space><space>CCC
7:<space><space>
8:DDD
output desired
1:AAA
2:BBB
3:<space><space><space>CCC
4:DDD
linux unix command-line-interface text parsing
linux unix command-line-interface text parsing
edited May 5 '15 at 17:04
kenorb
3,1733042
3,1733042
asked Mar 28 '11 at 22:14
Michael Ellick AngMichael Ellick Ang
5242514
5242514
1
For awk, see: Remove blank lines in awk, or using grep, in general, see: How to remove blank lines from a file in shell?
– kenorb
May 5 '15 at 16:11
same question and same answer given over here but with much more votes
– Trevor Boyd Smith
Feb 21 '18 at 19:18
add a comment |
1
For awk, see: Remove blank lines in awk, or using grep, in general, see: How to remove blank lines from a file in shell?
– kenorb
May 5 '15 at 16:11
same question and same answer given over here but with much more votes
– Trevor Boyd Smith
Feb 21 '18 at 19:18
1
1
For awk, see: Remove blank lines in awk, or using grep, in general, see: How to remove blank lines from a file in shell?
– kenorb
May 5 '15 at 16:11
For awk, see: Remove blank lines in awk, or using grep, in general, see: How to remove blank lines from a file in shell?
– kenorb
May 5 '15 at 16:11
same question and same answer given over here but with much more votes
– Trevor Boyd Smith
Feb 21 '18 at 19:18
same question and same answer given over here but with much more votes
– Trevor Boyd Smith
Feb 21 '18 at 19:18
add a comment |
7 Answers
7
active
oldest
votes
This sed line should do the trick:
sed -i '/^$/d' file.txt
The -i means it will edit the file in-place.
9
It actually needs to be "/^ *$/d" to remove lines that only contain spaces.
– Sean Reifschneider
Mar 28 '11 at 23:36
1
@SeanReifschneider That requirement was not in the question when this answer was written?
– kasperd
Apr 18 '16 at 10:02
3
@SeanReifschneider Would "/^s*$/d" not be better as it would include tabs? Whilst it's not mentioned in the original post, it seems a stronger option to me.
– mrswadge
Sep 20 '17 at 12:57
adding thes*is the same solution given by stackoverflow question
– Trevor Boyd Smith
Feb 21 '18 at 19:17
add a comment |
sed '/^$/d' file.txt
d is the sed command to delete a line. ^$ is a regular expression matching only a blank line, a line start followed by a line end.
+1 for the explanation
– Alex Raj Kaliamoorthy
Feb 17 '18 at 10:26
add a comment |
Simple solution by using grep (GNU or BSD) command:
grep . file
Similar with ripgrep (suitable for much larger files):
rg -N . file
2
grep .seems to be the simplest solution.
– Leo
Mar 21 '18 at 21:36
add a comment |
You can use the -v option with grep to remove the matching empty lines.
Like this
grep -Ev "^$" file.txt
3
I don't believe you need the-E, at least not with GNU grep, but apart from that I'm so pleased to see this done with grep! It's what I reach for in preference to sed, every time; in-line filters seem to me to be better than in-line editors.
– MadHatter
Mar 28 '11 at 22:45
If you want to skip the commented and blank lines, especially while dealing with conf files usegrep -Ev '^#|^$' file.txt
– Govind Kailas
Mar 7 at 4:11
add a comment |
To remove empty lines, you could squeeze new line repeats with tr:
cat file.txt | tr -s 'n' 'n'
add a comment |
Here is an awk solution:
awk NF file
With Awk, NF only set on non-blank lines. When this condition match, Awk default action is to print the whole line.
add a comment |
Ex/Vim
Here is the method using ex editor (part of Vim):
ex -s +'v/S/d' -cwq test.txt
For multiple files (edit in-place):
ex -s +'bufdo!v/S/d' -cxa *.txt
Note: The :bufdo command is not POSIX.
Without modifying the file (just print on the standard output):
cat test.txt | ex -s +'v/S/d' +%p +q! /dev/stdin
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "2"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fserverfault.com%2fquestions%2f252921%2fhow-to-remove-empty-blank-lines-in-a-file-in-unix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
This sed line should do the trick:
sed -i '/^$/d' file.txt
The -i means it will edit the file in-place.
9
It actually needs to be "/^ *$/d" to remove lines that only contain spaces.
– Sean Reifschneider
Mar 28 '11 at 23:36
1
@SeanReifschneider That requirement was not in the question when this answer was written?
– kasperd
Apr 18 '16 at 10:02
3
@SeanReifschneider Would "/^s*$/d" not be better as it would include tabs? Whilst it's not mentioned in the original post, it seems a stronger option to me.
– mrswadge
Sep 20 '17 at 12:57
adding thes*is the same solution given by stackoverflow question
– Trevor Boyd Smith
Feb 21 '18 at 19:17
add a comment |
This sed line should do the trick:
sed -i '/^$/d' file.txt
The -i means it will edit the file in-place.
9
It actually needs to be "/^ *$/d" to remove lines that only contain spaces.
– Sean Reifschneider
Mar 28 '11 at 23:36
1
@SeanReifschneider That requirement was not in the question when this answer was written?
– kasperd
Apr 18 '16 at 10:02
3
@SeanReifschneider Would "/^s*$/d" not be better as it would include tabs? Whilst it's not mentioned in the original post, it seems a stronger option to me.
– mrswadge
Sep 20 '17 at 12:57
adding thes*is the same solution given by stackoverflow question
– Trevor Boyd Smith
Feb 21 '18 at 19:17
add a comment |
This sed line should do the trick:
sed -i '/^$/d' file.txt
The -i means it will edit the file in-place.
This sed line should do the trick:
sed -i '/^$/d' file.txt
The -i means it will edit the file in-place.
edited 12 mins ago
Eddie C.
395211
395211
answered Mar 28 '11 at 22:19
Martijn HeemelsMartijn Heemels
5,89152757
5,89152757
9
It actually needs to be "/^ *$/d" to remove lines that only contain spaces.
– Sean Reifschneider
Mar 28 '11 at 23:36
1
@SeanReifschneider That requirement was not in the question when this answer was written?
– kasperd
Apr 18 '16 at 10:02
3
@SeanReifschneider Would "/^s*$/d" not be better as it would include tabs? Whilst it's not mentioned in the original post, it seems a stronger option to me.
– mrswadge
Sep 20 '17 at 12:57
adding thes*is the same solution given by stackoverflow question
– Trevor Boyd Smith
Feb 21 '18 at 19:17
add a comment |
9
It actually needs to be "/^ *$/d" to remove lines that only contain spaces.
– Sean Reifschneider
Mar 28 '11 at 23:36
1
@SeanReifschneider That requirement was not in the question when this answer was written?
– kasperd
Apr 18 '16 at 10:02
3
@SeanReifschneider Would "/^s*$/d" not be better as it would include tabs? Whilst it's not mentioned in the original post, it seems a stronger option to me.
– mrswadge
Sep 20 '17 at 12:57
adding thes*is the same solution given by stackoverflow question
– Trevor Boyd Smith
Feb 21 '18 at 19:17
9
9
It actually needs to be "/^ *$/d" to remove lines that only contain spaces.
– Sean Reifschneider
Mar 28 '11 at 23:36
It actually needs to be "/^ *$/d" to remove lines that only contain spaces.
– Sean Reifschneider
Mar 28 '11 at 23:36
1
1
@SeanReifschneider That requirement was not in the question when this answer was written?
– kasperd
Apr 18 '16 at 10:02
@SeanReifschneider That requirement was not in the question when this answer was written?
– kasperd
Apr 18 '16 at 10:02
3
3
@SeanReifschneider Would "/^s*$/d" not be better as it would include tabs? Whilst it's not mentioned in the original post, it seems a stronger option to me.
– mrswadge
Sep 20 '17 at 12:57
@SeanReifschneider Would "/^s*$/d" not be better as it would include tabs? Whilst it's not mentioned in the original post, it seems a stronger option to me.
– mrswadge
Sep 20 '17 at 12:57
adding the
s* is the same solution given by stackoverflow question– Trevor Boyd Smith
Feb 21 '18 at 19:17
adding the
s* is the same solution given by stackoverflow question– Trevor Boyd Smith
Feb 21 '18 at 19:17
add a comment |
sed '/^$/d' file.txt
d is the sed command to delete a line. ^$ is a regular expression matching only a blank line, a line start followed by a line end.
+1 for the explanation
– Alex Raj Kaliamoorthy
Feb 17 '18 at 10:26
add a comment |
sed '/^$/d' file.txt
d is the sed command to delete a line. ^$ is a regular expression matching only a blank line, a line start followed by a line end.
+1 for the explanation
– Alex Raj Kaliamoorthy
Feb 17 '18 at 10:26
add a comment |
sed '/^$/d' file.txt
d is the sed command to delete a line. ^$ is a regular expression matching only a blank line, a line start followed by a line end.
sed '/^$/d' file.txt
d is the sed command to delete a line. ^$ is a regular expression matching only a blank line, a line start followed by a line end.
answered Mar 28 '11 at 22:18
Kamil KisielKamil Kisiel
10k73765
10k73765
+1 for the explanation
– Alex Raj Kaliamoorthy
Feb 17 '18 at 10:26
add a comment |
+1 for the explanation
– Alex Raj Kaliamoorthy
Feb 17 '18 at 10:26
+1 for the explanation
– Alex Raj Kaliamoorthy
Feb 17 '18 at 10:26
+1 for the explanation
– Alex Raj Kaliamoorthy
Feb 17 '18 at 10:26
add a comment |
Simple solution by using grep (GNU or BSD) command:
grep . file
Similar with ripgrep (suitable for much larger files):
rg -N . file
2
grep .seems to be the simplest solution.
– Leo
Mar 21 '18 at 21:36
add a comment |
Simple solution by using grep (GNU or BSD) command:
grep . file
Similar with ripgrep (suitable for much larger files):
rg -N . file
2
grep .seems to be the simplest solution.
– Leo
Mar 21 '18 at 21:36
add a comment |
Simple solution by using grep (GNU or BSD) command:
grep . file
Similar with ripgrep (suitable for much larger files):
rg -N . file
Simple solution by using grep (GNU or BSD) command:
grep . file
Similar with ripgrep (suitable for much larger files):
rg -N . file
edited Jan 18 at 14:36
answered May 5 '15 at 16:12
kenorbkenorb
3,1733042
3,1733042
2
grep .seems to be the simplest solution.
– Leo
Mar 21 '18 at 21:36
add a comment |
2
grep .seems to be the simplest solution.
– Leo
Mar 21 '18 at 21:36
2
2
grep . seems to be the simplest solution.– Leo
Mar 21 '18 at 21:36
grep . seems to be the simplest solution.– Leo
Mar 21 '18 at 21:36
add a comment |
You can use the -v option with grep to remove the matching empty lines.
Like this
grep -Ev "^$" file.txt
3
I don't believe you need the-E, at least not with GNU grep, but apart from that I'm so pleased to see this done with grep! It's what I reach for in preference to sed, every time; in-line filters seem to me to be better than in-line editors.
– MadHatter
Mar 28 '11 at 22:45
If you want to skip the commented and blank lines, especially while dealing with conf files usegrep -Ev '^#|^$' file.txt
– Govind Kailas
Mar 7 at 4:11
add a comment |
You can use the -v option with grep to remove the matching empty lines.
Like this
grep -Ev "^$" file.txt
3
I don't believe you need the-E, at least not with GNU grep, but apart from that I'm so pleased to see this done with grep! It's what I reach for in preference to sed, every time; in-line filters seem to me to be better than in-line editors.
– MadHatter
Mar 28 '11 at 22:45
If you want to skip the commented and blank lines, especially while dealing with conf files usegrep -Ev '^#|^$' file.txt
– Govind Kailas
Mar 7 at 4:11
add a comment |
You can use the -v option with grep to remove the matching empty lines.
Like this
grep -Ev "^$" file.txt
You can use the -v option with grep to remove the matching empty lines.
Like this
grep -Ev "^$" file.txt
answered Mar 28 '11 at 22:20
jdabneyjdabney
1813
1813
3
I don't believe you need the-E, at least not with GNU grep, but apart from that I'm so pleased to see this done with grep! It's what I reach for in preference to sed, every time; in-line filters seem to me to be better than in-line editors.
– MadHatter
Mar 28 '11 at 22:45
If you want to skip the commented and blank lines, especially while dealing with conf files usegrep -Ev '^#|^$' file.txt
– Govind Kailas
Mar 7 at 4:11
add a comment |
3
I don't believe you need the-E, at least not with GNU grep, but apart from that I'm so pleased to see this done with grep! It's what I reach for in preference to sed, every time; in-line filters seem to me to be better than in-line editors.
– MadHatter
Mar 28 '11 at 22:45
If you want to skip the commented and blank lines, especially while dealing with conf files usegrep -Ev '^#|^$' file.txt
– Govind Kailas
Mar 7 at 4:11
3
3
I don't believe you need the
-E, at least not with GNU grep, but apart from that I'm so pleased to see this done with grep! It's what I reach for in preference to sed, every time; in-line filters seem to me to be better than in-line editors.– MadHatter
Mar 28 '11 at 22:45
I don't believe you need the
-E, at least not with GNU grep, but apart from that I'm so pleased to see this done with grep! It's what I reach for in preference to sed, every time; in-line filters seem to me to be better than in-line editors.– MadHatter
Mar 28 '11 at 22:45
If you want to skip the commented and blank lines, especially while dealing with conf files use
grep -Ev '^#|^$' file.txt– Govind Kailas
Mar 7 at 4:11
If you want to skip the commented and blank lines, especially while dealing with conf files use
grep -Ev '^#|^$' file.txt– Govind Kailas
Mar 7 at 4:11
add a comment |
To remove empty lines, you could squeeze new line repeats with tr:
cat file.txt | tr -s 'n' 'n'
add a comment |
To remove empty lines, you could squeeze new line repeats with tr:
cat file.txt | tr -s 'n' 'n'
add a comment |
To remove empty lines, you could squeeze new line repeats with tr:
cat file.txt | tr -s 'n' 'n'
To remove empty lines, you could squeeze new line repeats with tr:
cat file.txt | tr -s 'n' 'n'
answered Jul 26 '17 at 10:35
siddhadevsiddhadev
16111
16111
add a comment |
add a comment |
Here is an awk solution:
awk NF file
With Awk, NF only set on non-blank lines. When this condition match, Awk default action is to print the whole line.
add a comment |
Here is an awk solution:
awk NF file
With Awk, NF only set on non-blank lines. When this condition match, Awk default action is to print the whole line.
add a comment |
Here is an awk solution:
awk NF file
With Awk, NF only set on non-blank lines. When this condition match, Awk default action is to print the whole line.
Here is an awk solution:
awk NF file
With Awk, NF only set on non-blank lines. When this condition match, Awk default action is to print the whole line.
answered Jun 8 '18 at 16:48
Steven PennySteven Penny
1
1
add a comment |
add a comment |
Ex/Vim
Here is the method using ex editor (part of Vim):
ex -s +'v/S/d' -cwq test.txt
For multiple files (edit in-place):
ex -s +'bufdo!v/S/d' -cxa *.txt
Note: The :bufdo command is not POSIX.
Without modifying the file (just print on the standard output):
cat test.txt | ex -s +'v/S/d' +%p +q! /dev/stdin
add a comment |
Ex/Vim
Here is the method using ex editor (part of Vim):
ex -s +'v/S/d' -cwq test.txt
For multiple files (edit in-place):
ex -s +'bufdo!v/S/d' -cxa *.txt
Note: The :bufdo command is not POSIX.
Without modifying the file (just print on the standard output):
cat test.txt | ex -s +'v/S/d' +%p +q! /dev/stdin
add a comment |
Ex/Vim
Here is the method using ex editor (part of Vim):
ex -s +'v/S/d' -cwq test.txt
For multiple files (edit in-place):
ex -s +'bufdo!v/S/d' -cxa *.txt
Note: The :bufdo command is not POSIX.
Without modifying the file (just print on the standard output):
cat test.txt | ex -s +'v/S/d' +%p +q! /dev/stdin
Ex/Vim
Here is the method using ex editor (part of Vim):
ex -s +'v/S/d' -cwq test.txt
For multiple files (edit in-place):
ex -s +'bufdo!v/S/d' -cxa *.txt
Note: The :bufdo command is not POSIX.
Without modifying the file (just print on the standard output):
cat test.txt | ex -s +'v/S/d' +%p +q! /dev/stdin
answered Apr 12 '18 at 21:24
kenorbkenorb
3,1733042
3,1733042
add a comment |
add a comment |
Thanks for contributing an answer to Server Fault!
- 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%2fserverfault.com%2fquestions%2f252921%2fhow-to-remove-empty-blank-lines-in-a-file-in-unix%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
1
For awk, see: Remove blank lines in awk, or using grep, in general, see: How to remove blank lines from a file in shell?
– kenorb
May 5 '15 at 16:11
same question and same answer given over here but with much more votes
– Trevor Boyd Smith
Feb 21 '18 at 19:18