2 Answers
2

There are many tools you could use to do this. Here's a solution in perl.

On the command line:

perl -p -e 's/^ *//; s/ *$//; chomp' < FILE > OUTFILE

where you replace FILE with the name of the file and OUTFILE with the new file you want the output written to. Do not use the same filename for both.

What it does: perl -p runs a perl script on each line of the input it gets and writes the result to the output. You're setting the input and output with the < and > operators to the files you want. The script itself follows the -e option and does three substitutions.

s/^ *//: substitute any number of spaces (space, star) at the beginning of the line (^) with nothing (the command s/this/that/ changes this to that).
If you expect to have TAB characters instead of spaces, do s/^[ \t]*// which substitutes away any number of spaces or tabs (\t).