Delete blank spaces and blank lines in a file

Hi Gurus,
Somebody can say me how to delete blank spaces and blank lines in a file unix, please.

Thank you for advanced.

Well, that rather depends. There are several tools to do a variety of things, but can you show us some sample input & output wrapped in CODE tags for clarity.

It would be good to know the OS and version you are running so we don't suggest tools your won't have. The output from uname -a would be useful.

Regards,
Robin

Please be more precise - do you want to eliminate ALL spaces even though they might be field separators? What about <TAB>s? Are lines that contain nothing but white space to be considered "blank" lines?

Hi Gurus,
My file have spaces separators TAB and back spaces, I need one command for delete all spaces and all blank lines in this file.

Thank you for advanced.

... and do you want back-spaces retained or removed?

Can you show us the input and output? If there are non-printable characters, perhaps send the files through od to give us the character codes:-

od -x file

Robin

Hi Gurus,
I need to back-spaces removed, i am using operative system Solaris.

Thanks & Regards.

Hi,
Many examples with sed:

Delete blank line:

sed -e '/^[[:space:]]*$/d' file

Delete blank space:

sed -e 's/[[:space:]]//g' file

Replace several blank spaces by only one blank space

sed -e 's/\([[:space:]]\)[[:space:]]*/\1/g' file

Regards.

1 Like

We do not seem to be getting anywhere with this thread. And, it is obvious that we have a language barrier.

We do not understand what you are trying to do. The term "blank spaces" is confusing. The standards define <space> characters, <blank> characters (which includes <space> and <horizontal-tab> characters), <blank lines> (which are lines that contain zero or more <blank>s and a line terminating <newline> character), and <empty lines> (which are lines that just contain the line terminating <newline> character).

In post #6 in this thread you said "I need to back-spaces removed". But, <backspace> characters are not <blanks> and <backspace> characters do not appear on <blank lines>.

What are you trying to do?

  1. Do you want to remove every <space> character from a file?
  2. Do you want to remove every <horizontal-tab> character from a file?
  3. After removing every <space> and <horizontal-tab> character, do you want to remove every empty line from a file?
  4. Do you want to remove every <backspace> character from a file?
  5. Are there other characters you want to remove from a file?

How do you want to do it?

  1. Do you want to print a file to paper and cut these characters out with scissors?
  2. Do you want to make these changes using an interactive editor (such as vi )?
  3. Do you want to make these changes using a non-interactive editor (such as sed )?
  4. Do you want to make these changes using a tool in a pipeline (such as tr )?

Please give us some details about what you are trying to do so we can help you!

If its a text file and you want to remove blank lines, the quickest way to do this is

cat file |egrep -v '^$'

You need "egrep" but its probably installed.

No. This command will remove empty lines; not blank lines. And, you don't need egrep (or the currently preferred form grep -E ), for this, plain:

grep -v '^$' file

will remove empty lines from file . The command:

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

will remove blank lines from file .

And, there is no need to use cat here. Using cat takes more system resources and takes longer to get the results you want.

Another nice way to remove blank lines from a file is :

awk NF file

Although that will also not work if there are backspace characters (?) on that line.
@OP could you post a sample?