Uma solução usando sed e mantendo a primeira linha mais curta do arquivo:
sed -e '1h;H;g;s/[^\n]/#/g;s/\(#*\)\n/\n/;G;/^\n/s/\n.*\n\(.*\)\n.*//;s/.*\n//;h;$!d' your_file
Para manter a última linha mais curta do arquivo:
sed -e '1h;G;h;s/[^\n]/#/g;s/\(#*\)\n/\n/;G;/^\n/s/\n.*\n\(.*\)\n.*//;s/.*\n//;h;$!d' your_file
Abaixo está uma versão explicada da primeira linha mais curta na forma de um arquivo de script sed que pode ser executado usando sed -f script your_file
:
# The hold space will contain the shortest line at the beginning and the ending of each cycle.
# The 1st line is the shortest, so put it in the hold space so an empty line will not be returned.
1h
# Append the current line to the shortest so far, remember these 2 lines in the hold space, and take a copy in the pattern space to work on them.
H;g
# Replace all chars by #.
s/[^\n]/#/g
# Delete the same number of # before and after the line delimiter.
s/\(#*\)\n/\n/
# Append the 2 lines remembered in the hold space to the pattern space.
G
# If the hold space begin by a '\n', the current line was shorter, so keep it.
/^\n/s/\n.*\n\(.*\)\n.*//
# Else, the previous line was shorter, so keep it.
s/.*\n//
# Remember shortest in hold space.
h
# If last line, print it (delete everything else).
$!d