Shorten lines, adding "..." ellipsis

Solution 1:

This will truncate the string, chop off an additional three characters, and add "..." if the length is longer than the value you supply as a parameter.

other_programs | \
awk -v len=40 '{ if (length($0) > len) print substr($0, 1, len-3) "..."; else print; }'

Solution 2:

Try this:

awk -F '' '{if (NF > 70) {print substr($0, 0, 71)"..."} else print $0}'

If NF is too high, the simpler way:

awk '{if (length($0) > 70) {print substr($0, 0, 71)"..."} else print $0}'

or a shorter version:

awk 'length > 70{$0=substr($0,0,71)"..."}1'

Solution 3:

Some possibilities:

  • with sed

    sed -E 's/(.{N})(.{1,})$/\1.../' file
    
  • slightly more elegantly with perl (using lookbehind)

    perl -pe 's/(?<=.{N}).{1,}$/.../' file
    

where N is the number of characters after which you wish to replace with the ellipsis.