How Do I Remove Extra Spaces in Unix?
Contents

How do I get rid of extra spaces in Linux?

`sed` command is another option to remove leading and trailing space or character from the string data. The following commands will remove the spaces from the variable, $myVar using `sed` command. Use sed ‘s/^ *//g’, to remove the leading white spaces. There is another way to remove whitespaces using `sed` command.

How do I remove blank spaces in Unix?

Simple solution is by using grep (GNU or BSD) command as below.

  1. Remove blank lines (not including lines with spaces). grep . file.txt.
  2. Remove completely blank lines (including lines with spaces). grep “S” file.txt.
James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest