對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2016/8/25 12:17:20
How to remove blank lines from a file in shell?
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:
[: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...).
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.
Here is an awk solution:
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 :
or
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
You can use sed command for removing blank lines:
This command deletes all empty lines from the file "in"
Try ex-way:
For multiple files (edit in-place):
Note: The :bufdo command is not POSIX.
Without modifying the file (just print on the standard output):
Looks like I've found one not that fast, but funny at last:
原文出處:How to remove blank lines from a file in shell? - Unix & Linux Stack Exchange
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.
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
You can use sed command for removing blank lines:
sed '/^$/d' in > out
This command deletes all empty lines from the file "in"
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
Looks like I've found one not that fast, but funny at last:
| xargs -L1
原文出處:How to remove blank lines from a file in shell? - Unix & Linux Stack Exchange