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

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

问题描述

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

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

推荐答案

请尝试以下操作:

Try the following:

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行与\ r \ n样式行结尾),而上述只删除空行和unix样式行结尾的文件:

UPDATE: This works for me for a file with blank lines or "all white space" (such as windows lines with "\r\n" 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天全站免登陆