简体   繁体   English

通过 grep 删除文本文件中的空行

[英]Remove empty lines in a text file via grep

FILE : FILE

hello

world

foo

bar

How can I remove all the empty new lines in this FILE ?如何删除此FILE中的所有空新行?

Output of command:命令的输出:

FILE : FILE

hello
world
foo
bar

grep . FILE


(And if you really want to do it in sed, then: sed -e /^$/d FILE ) (如果您真的想在 sed 中执行此操作,则: sed -e /^$/d FILE

(And if you really want to do it in awk, then: awk /./ FILE ) (如果您真的想在 awk 中执行此操作,则: awk /./ FILE

请尝试以下操作:

grep -v -e '^$'
with awk, just check for number of fields. no need regex

$ more file
hello

world

foo

bar

$ awk 'NF' file
hello
world
foo
bar

这是一个删除所有空白或仅包含空格字符的行的解决方案:

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

If removing empty lines means lines including any spaces, use:如果删除空行意味着行包含任何空格,请使用:

grep '\S' FILE

For example:例如:

$  printf "line1\n\nline2\n \nline3\n\t\nline4\n" > FILE
$  cat -v FILE
line1

line2

line3

line4
$  grep '\S' FILE
line1
line2
line3
line4
$  grep . FILE
line1
line2

line3

line4

See also:也可以看看:

Try this: sed -i '/^[ \\t]*$/d' file-name试试这个: sed -i '/^[ \\t]*$/d' file-name

It will delete all blank lines having any no.它将删除所有没有任何空行。 of white spaces (spaces or tabs) ie (0 or more) in the file.文件中的空格(空格或制表符),即(0 个或更多)。

Note: there is a 'space' followed by '\\t' inside the square bracket.注意:方括号内有一个“空格”后跟“\\t”。

The modifier -i will force to write the updated contents back in the file.修饰符-i将强制将更新的内容写回到文件中。 Without this flag you can see the empty lines got deleted on the screen but the actual file will not be affected.如果没有这个标志,您可以看到屏幕上的空行被删除,但实际文件不会受到影响。

grep '^..' my_file

example例子

THIS

IS

THE

FILE

EOF_MYFILE

it gives as output only lines with at least 2 characters.它只提供至少有 2 个字符的行作为输出。

THIS
IS
THE
FILE
EOF_MYFILE

See also the results with grep '^' my_file outputs另请参阅grep '^' my_file输出的结果

THIS

IS

THE

FILE

EOF_MYFILE

and also with grep '^.' my_file还有grep '^.' my_file grep '^.' my_file outputs grep '^.' my_file输出

THIS
IS
THE
FILE
EOF_MYFILE

Try ex -way:尝试前方式

ex -s +'v/\S/d' -cwq test.txt

For multiple files (edit in-place):对于多个文件(就地编辑):

ex -s +'bufdo!v/\S/d' -cxa *.txt

Without modifying the file (just print on the standard output):不修改文件(只需在标准输出上打印):

cat test.txt | ex -s +'v/\S/d' +%p +q! /dev/stdin

Perl might be overkill, but it works just as well. Perl 可能有点矫枉过正,但它也能正常工作。

Removes all lines which are completely blank:删除所有完全空白的行:

perl -ne 'print if /./' file

Removes all lines which are completely blank, or only contain whitespace:删除所有完全空白或仅包含空格的行:

perl -ne 'print if ! /^\s*$/' file

Variation which edits the original and makes a .bak file:编辑原始文件并制作 .bak 文件的变体:

perl -i.bak -ne 'print if ! /^\s*$/' file

If you want to know what the total lines of code is in your Xcode project and you are not interested in listing the count for each swift file then this will give you the answer.如果您想知道 Xcode 项目中的总代码行数,并且您对列出每个 swift 文件的计数不感兴趣,那么这将为您提供答案。 It removes lines with no code at all and removes lines that are prefixed with the comment //它删除根本没有代码的行并删除以注释为前缀的行//

Run it at the root level of your Xcode project.在 Xcode 项目的根级别运行它。

find . \( -iname \*.swift \) -exec grep -v '^[[:space:]]*$' \+ | grep -v -e '//' | wc -l

If you have comment blocks in your code beginning with /* and ending with */ such as:如果您的代码中有以/*开头并以*/结尾的注释块,例如:

/*
 This is an comment block 
*/

then these will get included in the count.那么这些将被包括在计数中。 (Too hard). (太难)。

Simplest Answer -----------------------------------------最简单的答案 -----------------------------------------

[root@node1 ~]# cat /etc/sudoers | grep -v -e ^# -e ^$
Defaults   !visiblepw
Defaults    always_set_home
Defaults    match_group_by_gid
Defaults    always_query_group_plugin
Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
root    ALL=(ALL)       ALL
%wheel  ALL=(ALL)       ALL
[root@node1 ~]#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM