Remove empty lines in a text file via grep

LinuxGrepText Processing

Linux Problem Overview


FILE:

hello

world

foo

bar

How can I remove all the empty new lines in this FILE?

Output of command:

FILE:

hello
world
foo
bar

Linux Solutions


Solution 1 - Linux

grep . FILE

(And if you really want to do it in sed, then: sed -e /^$/d FILE)

(And if you really want to do it in awk, then: awk /./ FILE)

Solution 2 - Linux

Try the following:

grep -v -e '^$'

Solution 3 - Linux

with awk, just check for number of fields. no need regex

$ more file
hello

world

foo

bar

$ awk 'NF' file
hello
world
foo
bar

Solution 4 - Linux

Here is a solution that removes all lines that are either blank or contain only space characters:

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

Solution 5 - Linux

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:

Solution 6 - Linux

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

It will delete all blank lines having any no. of white spaces (spaces or tabs) i.e. (0 or more) in the file.

Note: there is a 'space' followed by '\t' inside the square bracket.

The modifier -i will force to write the updated contents back in the file. Without this flag you can see the empty lines got deleted on the screen but the actual file will not be affected.

Solution 7 - Linux

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 ~]#

Solution 8 - Linux

grep '^..' my_file

example

THIS

IS

THE

FILE

EOF_MYFILE

it gives as output only lines with at least 2 characters.

THIS
IS
THE
FILE
EOF_MYFILE

See also the results with grep '^' my_file outputs

THIS

IS

THE

FILE

EOF_MYFILE

and also with grep '^.' my_file outputs

THIS
IS
THE
FILE
EOF_MYFILE

Solution 9 - Linux

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

Solution 10 - Linux

Perl might be overkill, but it works just as well.

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:

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

Solution 11 - Linux

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. 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.

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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser191960View Question on Stackoverflow
Solution 1 - LinuxDigitalRossView Answer on Stackoverflow
Solution 2 - LinuxMr.ReeView Answer on Stackoverflow
Solution 3 - Linuxghostdog74View Answer on Stackoverflow
Solution 4 - LinuxMarco CoutinhoView Answer on Stackoverflow
Solution 5 - LinuxkenorbView Answer on Stackoverflow
Solution 6 - LinuxPrabhat Kumar SinghView Answer on Stackoverflow
Solution 7 - LinuxDeepak ShindeView Answer on Stackoverflow
Solution 8 - Linuxclblue2000View Answer on Stackoverflow
Solution 9 - LinuxkenorbView Answer on Stackoverflow
Solution 10 - LinuxChris KoknatView Answer on Stackoverflow
Solution 11 - LinuxChrispyView Answer on Stackoverflow