Remove Empty Lines in a Text File via Grep

Remove empty lines in a text file via grep

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)

Remove blank lines with grep

Try the following:

grep -v -e '^$' foo.txt

The -e option allows regex patterns for matching.

The single quotes around ^$ makes it work for Cshell. Other shells will be happy with either single or double quotes.

UPDATE: This works for me for a file with blank lines or "all white space" (such as windows lines with \r\n style line endings), whereas the above only removes files with blank lines and unix style line endings:

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

remove all empty lines from text files while keeping format

  • Ctrl+H
  • Find what: \R^$
  • Replace with: LEAVE EMPTY
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

\R      : any kind of linebreak
^ : begining of line
$ : end of line

Result for given example:

apples
oranges
peaches

Delete empty lines from a text file via Bash including empty spaces characters

Use character class [:blank:] to indicate space or tab:

With sed:

sed -i '/^[[:blank:]]*$/ d' file.txt

With perl:

perl -ne 'print if !/^[[:blank:]]*$/' file.txt 

With awk:

awk '!/^[[:blank:]]*$/' file.txt 

With grep:

grep -v '^[[:blank:]]*$' file.txt

If the tool does not support editing in-place, leverage a temporary file e.g. for grep:

grep -v '^[[:blank:]]*$' file.txt >file.txt.tmp && mv file.txt{.tmp,}

How to remove empty lines to one empty line between sentences in text files?

You can use awk for this:

awk 'BEGIN{prev="x"}
/^$/ {if (prev==""){next}}
{prev=$0;print}' inputFile

or the compressed one liner:

awk 'BEGIN{p="x"}/^$/{if(p==""){next}}{p=$0;print}' inFl

This is a simple state machine that collapses multi-blank-lines into a single one.

The basic idea is this. First, set the previous line to be non-empty.

Then, for every line in the file, if it and the previous one are blank, just throw it away.

Otherwise, set the previous line to that value, print the line, and carry on.

Sample transcript, the following command:

$ echo '1
2
3
4
5

6
7
8
9

10' | awk 'BEGIN{p="x"}/^$/{if(p==""){next}}{p=$0;print}'

outputs:

1
2
3
4
5

6
7
8
9

10

Keep in mind that this is for truly blank lines (no content). If you're trying to collapse lines that have an arbitrary number of spaces or tabs, that will be a little trickier.

In that case, you could pipe the file through something like:

sed 's/^\s*$//'

to ensure lines with just whitespace become truly empty.

In other words, something like:

sed 's/^\s*$//' infile | awk 'my previous awk command'

Remove blank lines from the ends of a bunch of files

With Perl:

perl -0777 -pe 's/\n*$//; s/$/\n/' file

Second S command (s/$/\n/) appends again a newline to end of your file to be POSIX compilant.

Or shorter:

perl -0777 -pe 's/\n*$/\n/' file

With Fela Maslen's comment to edit files in place (-i) and glob all elements in current directory (*):

perl -0777 -pe 's/\n*$/\n/' -i *

How to print a file, excluding comments and blank lines, using grep/sed?

With grep:

grep -v '^\s*$\|^\s*\#' temp

On OSX / BSD systems:

grep -Ev '^\s*$|^\s*\#' temp

Remove blank lines in a file using sed

Use the following sed to delete all blank lines.

sed '/./!d' cou.data

Explanation:

  • /./ matches any character, including a newline.
  • ! negates the selector, i.e. it makes the command apply to lines which do not match the selector, which in this case is the empty line(s).
  • d deletes the selected line(s).
  • cou.data is the path to the input file.

Where did you go wrong?

The following excerpt from How sed Works states:

sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.

When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.8 Then the next cycle starts for the next input line.

I've intentionally emboldened the parts which are pertinent to why your sed examples are not working. Given your examples:

  • They seem to disregard that sed reads one line at a time.
  • The trailing newlines, (\n\n and \n\n\n in your first and second example respectively), which you're trying to match don't actually exist. They've been removed by the time your regexp pattern is executed and then reinstated when the end of the script is reached.


Related Topics



Leave a reply



Submit