bash 检查文件中的空行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20785564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:06:09  来源:igfitidea点击:

Checking for empty lines in a file

bashfileunix

提问by littlerunaway

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"?

如何检查一行是否“合法为空”?

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?

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

So I've tried something like this:

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

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

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

但它不起作用。如果我在一个空行中放置一个“”,它仍然认为它是空的。

回答by Vijay

If you want the line numbers of those empty lines:

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

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

If you want to delete those lines without Perl:

如果你想在没有 Perl 的情况下删除这些行:

grep . your_file >new_file

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

如果你想使用 Perl 删除那些空行:

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

回答by Gilles 'SO- stop being evil'

Terminology: a line that contains only white space is a blank line. A line that contains nothing (except for the newline terminator) is an empty line.

术语:仅包含空格的行空行。不包含任何内容(换行符除外)的行是空行

The readbuiltin strips off leading and trailing whitespace. So if it encounters a blank line, it sets its argument to an empty string, regardless of the amount of whitespace. To avoid this behavior and return the input line unmodified, set the field separator characters to nothing (by default, they are space, tab and newline): set the IFSvariable to the empty string. See Why is while IFS= readused so often, instead of IFS=; while read..?for a more detailed explanation. While you're at it, pass the -roption to read, unless you want backslash-newline sequences to be a line continuation.

read内置剥去开头和结尾的空白。因此,如果遇到空行,它会将其参数设置为空字符串,而不管空格的数量。为避免这种行为并返回未修改的输入行,请将字段分隔符设置为空(默认情况下,它们是空格、制表符和换行符):将IFS变量设置为空字符串。请参阅为什么while IFS= read如此频繁地使用,而不是IFS=; while read..? 以获得更详细的解释。在此过程中,将-r选项传递给read,除非您希望反斜杠换行序列成为行的延续。

while IFS= read -r line; do
  if [ -z "$line" ]; then
    echo empty line
  fi
done <""

If you want to tell whether a line is blank:

如果要判断一行是否为空:

while IFS= read -r line; do
  case "$line" in
    '') echo "empty line";;
    *[![:space:]]*) echo "non-blank line";;
    *) echo "non-empty blank line";;
  esac
done <""

You can use Bash regular expression matching if you prefer:

如果您愿意,可以使用 Bash 正则表达式匹配:

while IFS= read -r line; do
  if [[ "$line" =~ ^$ ]]; then
     echo "empty line"
  elif [[ "$line" =~ ^[[:space:]]+$ ]]; then
    echo "non-empty blank line"
  else
    echo "non-blank line"
  fi
done <""

These can be done with pattern matching too (using shell wildcards, which have a different syntax from common regular expressions):

这些也可以通过模式匹配来完成(使用 shell 通配符,它​​的语法与常见的正则表达式不同):

while IFS= read -r line; do
  if [[ "$line" == "" ]]; then
     echo "empty line"
  elif [[ "$line" != *[![:space:]]* ]]; then
    echo "non-empty blank line"
  else
    echo "non-blank line"
  fi
done <""

If you merely want to look for empty lines in the file and aren't processing the lines in any other way, you can use grep:

如果您只想在文件中查找空行并且不以任何其他方式处理这些行,您可以使用 grep:

if grep -qxF '' <""; then
  echo " contains an empty line"
fi

If you're looking for blank lines that are not empty:

如果您正在寻找非空的空行:

if grep -Ex '[[:space:]]+' <""; then
  echo " contains a non-empty blank line"
fi

回答by pfnuesel

You can check for an empty line with the regex

您可以使用正则表达式检查空行

^$

^is the beginning of a line, $is the end of a line, the above regexmatches if there are no other characters.

^是一行的开始,是一行$的结束,如果没有其他字符,则上述正则表达式匹配。

You can now use that in e.g. sed

您现在可以在例如sed 中使用它

sed '/^$/d' input.txt

This would delete all empty lines from your input file.This would remove empty lines from the file and display the file content on console. The file still remains unchanged.

这将从您的输入文件中删除所有空行。这将从文件中删除空行并在控制台上显示文件内容。该文件仍然保持不变。

If you want to remove the empty lines from the file (meaning, changing the file content), then run:

如果要从文件中删除空行(即更改文件内容),请运行:

sed -i '/^$/d' input.txt