I want to remove all empty lines from a file. Even if the line contains spaces or tabs it should also be removed.
Just grep
for non-blanks:
grep '[^[:blank:]]' < file.in > file.out
[:blank:]
, inside character ranges ([...]
), is called a POSIX character class. There are a few like [:alpha:]
, [:digit:]
... [:blank:]
matches horizontal white space (in the POSIX locale, that's space and tab, but in other locales there could be more, like all the Unicode horizontal spacing characters in UTF8 locales) while [[:space:]]
matches horizontal and vertical white space characters (same as [:blank:]
plus things like vertical tab, form feed...).
grep '[:blank:]'
Would return the lines that contain any of the characters, :
, b
, l
, a
, n
or k
. Character classes are only recognised within [...]
, and ^
within [...]
negates the set. So [^[:blank:]]
means any character but the blank ones.
-
1
-
-
1@MichaelDurrant.
[^[:blank:]]$
would only match lines that end in a non-blank. We want lines that contain a non-blank anywhere – Stéphane Chazelas Nov 16 '13 at 21:11 -
@StephaneChazelas I tried grep [:blank:] SOURCEFILE even this command is working. I understand [] is for character class can you please give me some idea on how it works ? the snippet :blank: is new to me. – Jamshed Ansari user3000272 Nov 16 '13 at 21:52
-
Here is an awk
solution:
$ awk NF file
With awk
, NF
only set on non-blank lines. When this condition match, awk
default action that is print
will print the whole line.
How about:
sed -e 's/^[[:blank:]]*$//' source_file > newfile
or
sed -e '/^[[:blank:]]*$/d' source_file > newfile
i.e.
For each line, substitute:
- if it starts ("
^
") - with spaces or tabs ("
[[:blank:]]
") zero or more times ("*
") - and then is the end of the line ("
$
")
More info on ::blank:: and other special characters at http://www.zytrax.com/tech/web/regex.htm#special
-
4
[[:space:]]
includes tabs. If it didn't your regex would fail if a space followed a tab. – jordanm Nov 16 '13 at 21:06 -
The
wctype(3)
andisalpha(3)
manpages describe what the character classes will match. – jordanm Nov 16 '13 at 21:10 -
You may want to remove the first one which doesn't answer the question. – Stéphane Chazelas Nov 16 '13 at 21:33
-
@MichaelDurrant can you please write some thing about [[:blank:]] ? – Jamshed Ansari user3000272 Nov 16 '13 at 22:00
-
Added info for [[:blank::]]. Stephane, why doesn't the first work? I thought // at the end would replace the line without nothing. – Michael Durrant Nov 16 '13 at 22:56
You can use sed
command for removing blank lines:
sed '/^$/d' in > out
This command deletes all empty lines from the file "in"
-
That doesn't delete lines containing only space and tab, as specifically requested. – dave_thompson_085 Jun 15 '16 at 20:15
Looks like I've found one not that fast, but funny at last:
| xargs -L1
-
1Nice short one, but it does more: remove leading spaces and tabs as well. – jringoot Jul 4 '17 at 5:25
-
-
1And it truncates lines (default at 1024 chars) See manpages: linux.die.net/man/1/xargs – jringoot Jul 5 '17 at 9:34
-
Try ex-way:
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
-
note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html – Steven Penny Apr 17 '16 at 0:44
Use the following command:
grep '\S' FILE
which removes all lines including spaces or tabs.
Otherwise, removal not including lines with spaces/tabs, use:
grep . FILE
For example:
$ printf "line1\n\nline2\n \nline3\n" > FILE
$ cat -v FILE
line1
line2
line3
$ grep '\S' FILE
line1
line2
line3
$ grep . FILE
line1
line2
line3
See also:
- Removal including space/tab lines: How to remove empty/blank lines in a file in Unix?
- With
sed
: Delete empty lines using sed - With
awk
: Remove blank lines using awk