Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Linux - Delete Empty Lines Using Sed - Stack Overflow

linux - Delete empty lines using sed - Stack Overflow

Uploaded by

Sathi Natarajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Linux - Delete Empty Lines Using Sed - Stack Overflow

linux - Delete empty lines using sed - Stack Overflow

Uploaded by

Sathi Natarajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow

Delete empty lines using sed


Asked 11 years, 6 months ago Modified 1 month ago Viewed 726k times

I am trying to delete empty lines using sed:

516 sed '/^$/d'

but I have no luck with it.

For example, I have these lines:

xxxxxx

yyyyyy

zzzzzz

and I want it to be like:

xxxxxx
yyyyyy
zzzzzz

What should be the code for this?

linux unix sed

Share Improve this question Follow edited Aug 10, 2018 at 13:47 asked May 7, 2013 at 8:20
Benjamin W. jonas
51.3k 19 125 131 5,459 4 17 11

5 your sed command looks fine, it should work – perreal May 7, 2013 at 8:23

The above command wouldn't work even if you don't have a space/tab but CR+LF line endings. – devnull
May 7, 2013 at 8:53

2 For awk, see: Remove blank lines in awk, or using grep, in general, see: How to remove blank lines from a
file in shell? – kenorb May 5, 2015 at 16:07

https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 1/9
11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow

16 Answers Sorted by: Highest score (default)

You may have spaces or tabs in your "empty" line. Use POSIX classes with sed to remove all
lines containing only whitespace:
835
sed '/^[[:space:]]*$/d'

A shorter version that uses ERE, for example with gnu sed:

sed -r '/^\s*$/d'

(Note that sed does NOT support PCRE.)

Share Improve this answer Follow edited Nov 27, 2018 at 11:46 answered May 7, 2013 at 8:24
Kent
194k 35 242 311

3 @HuStmpHrrr gnu sed doesn't support PCRE at all. it is ERE with -r – Kent Feb 17, 2015 at 3:19

22 OS X needed sed -i "" '/^[[:space:]]*$/d' <filename> , – jww Oct 4, 2016 at 19:04

2 @BernieReiter ^\s*$ will match all "empty" lines, empty here means, the line contains no chars, or the
line contains only empty strings (E.g. spaces). All matched lines will be removed by sed, with the d
command. – Kent Feb 26, 2017 at 19:41

Perhaps sed '/\S/!d' file – potong Jul 30, 2020 at 10:59

I didn't need the -r flag on (GNU sed) 4.9 – Pound Hash Sep 8, 2023 at 19:19

I am missing the awk solution:

214 awk 'NF' file

Which would return:

xxxxxx
yyyyyy
zzzzzz

How does this work? Since NF stands for "number of fields", those lines being empty have 0
fields, so that awk evaluates 0 to False and no line is printed; however, if there is at least one
field, the evaluation is True and makes awk perform its default action: print the current line.

Share Improve this answer Follow edited Jun 11, 2022 at 22:37 answered Apr 9, 2015 at 21:35
https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 2/9
11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow
Ramy fedorqui
21.2k 48 105 157 288k 108 584 624

1 Whoah. Is even running with BSD's "minimized" version of awk (version 20121220 (FreeBSD). Thanks :-)
– Bernie Reiter Feb 25, 2017 at 0:36

@BernieReiter you are welcome :) Yes, this is a very basic idiomatic thing all awk versions allow.
– fedorqui Feb 26, 2017 at 20:43

And it is so much quicker although - for a quick and dirty test - I am invoking awk twice: $ time (topic
companies <data.tpx | awk 'NF' - | awk -f dialog_menu.awk -) real 0m0.006s
user 0m0.000s sys 0m0.008s $ time (topic companies <data.tpx | gsed
'/^\s*$/d' | awk -f dialog_menu.awk -) real 0m0.014s user 0m0.002s sys
0m0.006s Would you know of a nifty way to include this into an awk-script like, e.g., a pattern? awk
'/mypattern/ {do stuff...}' – Bernie Reiter Feb 27, 2017 at 22:17

2 Note that this will also ignore lines with whitespace only. – wisbucky Apr 25, 2019 at 22:47

1 Doesn't work if empty lines also contain carriage return characters (\r) – ychaouche Aug 17, 2021 at
12:21

sed
'/^[[:space:]]*$/d'
104
'/^\s*$/d'

'/^$/d'

-n '/^\s*$/!p'

grep
.

-v '^$'

-v '^\s*$'

-v '^[[:space:]]*$'

awk
/./

'NF'

'length'

'/^[ \t]*$/ {next;} {print}'

'!/^[ \t]*$/'

https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 3/9
11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow
Share Improve this answer Follow edited Apr 25, 2019 at 22:37 answered Aug 25, 2016 at 7:37
wisbucky Oleg Mazko
37.4k 12 163 112 1,820 1 16 10

2 These show up correctly in your online tool, but [] should not be escaped in a bracket expression, so the
code here isn't correct for \[\[:space:\]\] or \[ \t\] - should be [[:space:]] and [ \t] .
– Benjamin W. Aug 10, 2018 at 13:52

1 @BenjaminW. Thanks for catching that. Those were not from the original author, but came from Edit 3
when it was changed from regular text to "code", which then "exposed" the `\` escaping. I have fixed them
now. – wisbucky Apr 25, 2019 at 22:41

sed '/^$/d' should be fine, are you expecting to modify the file in place? If so you should use
the -i flag.
86
Maybe those lines are not empty, so if that's the case, look at this question Remove empty lines
from txtfiles, remove spaces from start and end of line I believe that's what you're trying to
achieve.

Share Improve this answer Follow edited May 23, 2017 at 12:10 answered May 7, 2013 at 8:23
Community Bot Alberto Zaccagni
1 1 31.5k 11 74 107

yes. i am modifying a file. *.csv. how should the -i be placed to the sed command? – jonas May 7, 2013
at 8:28

5 sed -i '/^$/d' is one way of doing it. – Alberto Zaccagni May 7, 2013 at 8:31

I believe this is the easiest and fastest one:

45 cat file.txt | grep .

If you need to ignore all white-space lines as well then try this:

cat file.txt | grep '\S'

Example:

s="\
\
a\
b\
\
Below is TAB:\
\
https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 4/9
11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow
Below is space:\
\
c\
\
"; echo "$s" | grep . | wc -l; echo "$s" | grep '\S' | wc -l

outputs

7
5

Share Improve this answer Follow answered Oct 17, 2014 at 3:26
Vadim
829 6 10

13 No need for cat , grep takes files as well: grep . file.txt – Ciro Santilli OurBigBook.com May 16,
2016 at 15:35

3 Yes, I know, but the initial question did not mention whether the source is a file or something else, so the
solution is what comes after "|", and before it just an example of a source. Simply to distinguish the
solution from the source of lines. – Vadim May 17, 2016 at 16:33

2 grep '\S' is definitely not portable. If you have grep -P then you can use grep -P '\S' but it's
not supported on all platforms, either. – tripleee Jan 9, 2017 at 6:53

The downside of grep . compared to the other solutions is that it will highlight all the text in red. The
other solutions can preserve the original colors. Compare unbuffer apt search foo | grep . to
unbuffer apt search foo | grep -v ^$ – wisbucky Apr 25, 2019 at 23:12

@wisbucky grep does not default to color output, but often it's enable via a shell alias or environment
variable. Use grep --color=never . to override. – Clement Cherlin Oct 6, 2022 at 17:28

https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 5/9
11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow

Another option without sed , awk , perl , etc

19 strings $file > $output

strings - print the strings of printable characters in files.

Share Improve this answer Follow edited May 2, 2020 at 12:17 answered May 1, 2020 at 20:45
user319660
322 3 10

1 Do you mean strings instead of string ? – Mickael B. May 2, 2020 at 1:06

2 "For each file given, GNU strings prints the printable character sequences that are at least 4 characters
long..." so very short lines might give you a surprise if you're unaware of this. There is a --bytes=min-
len option to allow shorter lines. – SouthwindCG Apr 24, 2022 at 4:45

With help from the accepted answer here and the accepted answer above, I have used:

17 $ sed 's/^ *//; s/ *$//; /^$/d; /^\s*$/d' file.txt > output.txt

`s/^ *//` => left trim


`s/ *$//` => right trim
`/^$/d` => remove empty line
`/^\s*$/d` => delete lines which may contain white space

This covers all the bases and works perfectly for my needs. Kudos to the original posters @Kent
and @kev

Share Improve this answer Follow edited May 23, 2017 at 11:54 answered Jul 25, 2014 at 14:10
Community Bot ConMan
1 1 1,652 1 14 23

The command you are trying is correct, just use -E flag with it.

9 sed -E '/^$/d'

-E flag makes sed catch extended regular expressions. More info here

Share Improve this answer Follow answered May 8, 2021 at 16:45


Samuel Kenneth
109 2 2

https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 6/9
11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow

There is nothing in this particular regex which requires the -E flag. – tripleee May 10, 2021 at 9:44

You can say:

4 sed -n '/ / p' filename #there is a space between '//'

Share Improve this answer Follow edited Apr 2, 2015 at 12:50 answered May 27, 2014 at 10:06
fedorqui tank
288k 108 584 624 61 1 4

.. which means print all lines except the empty one(s) and be quiet – Timo Feb 24, 2018 at
11:45

Actually, no; this means print the ones with at least one space in them. – tripleee Oct 7 at 4:52

You are most likely seeing the unexpected behavior because your text file was created on
Windows, so the end of line sequence is \r\n . You can use dos2unix to convert it to a UNIX
4 style text file before running sed or use

sed -r "/^\r?$/d"

to remove blank lines whether or not the carriage return is there.

Share Improve this answer Follow edited Aug 10, 2018 at 13:53 answered Mar 4, 2017 at 5:12
Benjamin W. Douglas Daseeco
51.3k 19 125 131 3,671 24 29

Hi, what is the -r flag doing and is it possible to combine it with -i to modify the file directly and avoid
printing to screen. In addition, I think that this command would also work as sed -r "/^\r$/d"
– Alexander Cska Nov 25, 2018 at 12:34

This works in awk as well.

3 awk '!/^$/' file


xxxxxx
yyyyyy
zzzzzz

Share Improve this answer Follow answered Aug 25, 2016 at 11:03

https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 7/9
11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow
Claes Wikner
1,497 1 9 8

You can do something like that using "grep", too:

2 egrep -v "^$" file.txt

Share Improve this answer Follow answered Aug 11, 2014 at 17:20
Lowbit
21 2

If you want to use modern Rust tools, you can consider:

ripgrep:
2
cat datafile | rg '.' line with spaces is considered non empty

cat datafile | rg '\S' line with spaces is considered empty

rg '\S' datafile line with spaces is considered empty ( -N can be added to remove
line numbers for on screen display)

sd

cat datafile | sd '^\n' '' line with spaces is considered non empty

cat datafile | sd '^\s*\n' '' line with spaces is considered empty

sd '^\s*\n' '' datafile inplace edit

Share Improve this answer Follow answered Dec 19, 2021 at 17:27
Kpym
3,993 1 24 18

Avoid the useless cat – tripleee Oct 7 at 4:49

My bash -specific answer is to recommend using perl substitution operator with the global
pattern g flag for this, as follows:
1
$ perl -pe s'/^\n|^[\ ]*\n//g' $file
xxxxxx
yyyyyy
zzzzzz

https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 8/9
11/22/24, 11:21 PM linux - Delete empty lines using sed - Stack Overflow

This answer illustrates accounting for whether or not the empty lines have spaces in them ( [\
]*), as well as using | to separate multiple search terms/fields. Tested on macOS High Sierra
and CentOS 6/7.

FYI, the OP's original code sed '/^$/d' $file works just fine in bash Terminal on macOS High
Sierra and CentOS 6/7 Linux at a high-performance supercomputing cluster.

Share Improve this answer Follow edited Jul 24, 2018 at 22:20 answered Jul 24, 2018 at 21:34
justincbagley
11 4

Using vim editor to remove empty lines

:%s/^$\n//g
-1
Share Improve this answer Follow answered Apr 9, 2021 at 3:36
Nilesh Shukla
11 1

For me with FreeBSD 10.1 with sed worked only this solution:

-2 sed -e '/^[ ]*$/d' "testfile"

inside [] there are space and tab symbols.

test file contains:

fffffff next 1 tabline ffffffffffff

ffffffff next 1 Space line ffffffffffff

ffffffff empty 1 lines ffffffffffff

============ EOF =============

Share Improve this answer Follow edited Aug 16, 2017 at 14:44 answered Mar 17, 2017 at 14:19
fedorqui Vitaly
288k 108 584 624 15 3

https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed 9/9

You might also like