I would like to remove all of the empty lines from a file, but only when they are at the end/start of a file (that is, if there are no non-empty lines before them, at the start;
this can be solved easily with sed -z option
-z
sed -rz 's/^\n+//; s/\n+$/\n/g' file Hello Welcome to Unix and Linux