May 2007
Beginner
628 pages
15h 46m
English
You need to trim leading and/or trailing whitespace from lines for fields of data.
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 ...Read now
Unlock full access