检查文件中的空行 [英] Checking for empty lines in a file

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

问题描述

我这里没有代码示例,因为我不确定如何执行此操作,但是我有一个文件.合法的空行是仅包含换行标签的行.空格或制表符是非法的.

I don't have a code example here since I'm not sure how to do this at all, but I have a file. A legal empty line is one that only contains the new-line tab. Spaces or tabs are illegal.

如何检查行是否合法为空"?

How do I check if a line is "legally empty"?

如果它没有任何单词(我可以用wc -w进行检查),如何检查它是否也没有空格或制表符,只是换行符?

If it doesn't have any words (I can check this with wc -w), how do I check if it has no spaces or tabs either, just new-line?

所以我尝试过这样的事情:

So I've tried something like this:

while read line; do
    if [[ "$line" =~ ^$ ]]; then
        echo empty line
        continue
    fi
done < $1

但是它不起作用.如果我将"放在否则为空的行中,它仍会认为它为空.

But it's not working. If I put a " " in an otherwise empty line, it still considers it empty.

推荐答案

如果您想要这些空行的行号:

If you want the line numbers of those empty lines:

perl -lne 'print $. if(/^$/)' your_file

如果要删除没有Perl的那些行:

If you want to delete those lines without Perl:

grep . your_file >new_file

如果要使用Perl删除这些空行:

If you want to delete those empty line in place using Perl:

perl -i -lne 'print if(/./)' your_file

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

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