Remove empty lines from txtfiles, remove spaces from start and end of line

alvas picture alvas · Dec 19, 2011 · Viewed 21.8k times · Source

Which one would be better:

sed -e '/^$/d' *.txt
sed 'g/^$/d' -i *.txt

Also, how do I remove spaces from beginning and end of each line in the text file?

Answer

kev picture kev · Dec 19, 2011
$ sed 's/^ *//; s/ *$//; /^$/d' file.txt

`s/^ *//`  => left trim
`s/ *$//`  => right trim
`/^$/d`    => remove empty line