Remove blank lines with grep

2019-03-07 16:17发布

I tried grep -v '^$' in Linux and that didn't work. This file came from a Windows file system.

14条回答
成全新的幸福
2楼-- · 2019-03-07 16:42
awk 'NF' file-with-blank-lines > file-with-no-blank-lines
查看更多
够拽才男人
3楼-- · 2019-03-07 16:43

Tried hard but this seems to work (assuming \r is biting you here)

printf "\r" | egrep -xv "[[:space:]]*"
查看更多
Melony?
4楼-- · 2019-03-07 16:45

You can remove blank line with this example:

grep . filename.txt
查看更多
Lonely孤独者°
5楼-- · 2019-03-07 16:47

egrep -v "^\s\s+"

egrep already do regex, and the \s is white space.

The + duplicates current pattern.

The ^ is for the start

查看更多
看我几分像从前
6楼-- · 2019-03-07 16:48

grep pattern filename.txt | uniq

查看更多
唯我独甜
7楼-- · 2019-03-07 16:49
grep -v "^[[:space:]]*$"

The -v makes it print lines that do not completely match

===Each part explained===
^             match start of line
[[:space:]]   match whitespace- spaces, tabs, carriage returns, etc.
*             previous match (whitespace) may exist from 0 to infinite times
$             match end of line

Running the code-

$ echo "
> hello
>       
> ok" |
> grep -v "^[[:space:]]*$"
hello
ok

To understand more about how/why this works, I recommend reading up on regular expressions. http://www.regular-expressions.info/tutorial.html

查看更多
登录 后发表回答