Two sed commands in one command

sed

I have a file with a lots of names etc. and with spaces in between. Now, I want to delete all trailing spaces and all empty lines in this file with sed.

I have two commands for this task but I would like to have a combination of both:

sed -i's/\s*$//g' 
sed -i'/^$/d'

Best Answer

With GNU sed (and probably others), you can give multiple commands separated by a semicolon:

sed -i 's/\s*$//g; /^$/d'

Other sed implementations might need you to give the two commands separately with -e:

sed -i -e 's/\s*$//g' -e '/^$/d'

Finally, you could also combine them into a sed script:

$ cat foo.sed
s/\s*$//g
/^$/d

Then, you run it with -f:

$ sed -i -f foo.sed file
Related Question