Cover by Cameron Newham, JP Vossen, Carl Albing

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Trimming Whitespace

Problem

You need to trim leading and/or trailing whitespace from lines for fields of data.

Solution

These solutions rely on a bash- specific treatment of read and $REPLY. See the end of the discussion for an alternate solution.

First, we'll show a file with some leading and trailing whitespace. Note we add ~~ to show the whitespace. Note the → denotes a literal tab character in the output:

	# Show the whitespace in our sample file
	$ while read; do echo ~~"$REPLY"~~; done < whitespace
	~~ This line has leading spaces.~~
	~~This line has trailing spaces. ~~
	~~ This line has both leading and trailing spaces. ~~
	~~ → Leading tab.~~
	~~Trailing tab. → ~~
	~~ → Leading and trailing tab. → ~~
	~~ → Leading mixed whitespace.~~
	~~Trailing mixed whitespace. →     ~~
	~~ → Leading and trailing mixed whitespace. →     ~~

To trim both leading and trailing whitespace use $IFS add the built-in REPLY variable (see the discussion for why this works):

	$ while readREPLY; do echo ~~"$REPLY"~~; done < whitespace
	~~This line has leading spaces.~~
	~~This line has trailing spaces.~~
	~~This line has both leading and trailing spaces.~~
	~~Leading tab.~~
	~~Trailing tab.~~
	~~Leading and trailing tab.~~
	~~Leading mixed whitespace.~~
	~~Trailing mixed whitespace.~~
	~~Leading and trailing mixed whitespace.~~

To trim only leading or only trailing spaces, use a simple pattern match:

	# Leading spaces only
	$ while read; do echo "~~${REPLY## }~~"; done < whitespace ~~This line has leading spaces.~~ ~~This line has trailing ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required