用 grep 删除空行 [英] Remove blank lines with grep

查看:46
本文介绍了用 grep 删除空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Linux 中尝试了 grep -v '^$' 但没有奏效.此文件来自 Windows 文件系统.

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

推荐答案

尝试以下操作:

grep -v -e '^$' foo.txt

-e 选项允许匹配正则表达式.

The -e option allows regex patterns for matching.

^$ 周围的单引号使其适用于 Cshell.其他 shell 会对单引号或双引号感到满意.

The single quotes around ^$ makes it work for Cshell. Other shells will be happy with either single or double quotes.

更新:这对我来说适用于带有空行或全空白"的文件(例如带有 "样式行结尾的 Windows 行),而上述仅删除带有空行和 unix 样式的文件行尾:

UPDATE: This works for me for a file with blank lines or "all white space" (such as windows lines with " " style line endings), whereas the above only removes files with blank lines and unix style line endings:

grep -v -e '^[[:space:]]*$' foo.txt

这篇关于用 grep 删除空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆