Multiple strings, Truncate line at 80 characters

TZPike05 picture TZPike05 · Nov 6, 2013 · Viewed 40.9k times · Source

I'm new to awk and sed, and I'm looking for a way to truncate a line at 80 characters, but I'm printing several strings in that line using printf. The last two strings are the ones that give me problems because they vary in size on each iteration of my code. Here's my current code:

printf "%5d  %3s%.2s %4s %s %s \n" "$f" "$month" "$day" "$year" "$from" "$subject"

This code is being used to create a summary of email messages that get passed through a Bash script. What I do know, is that with the spaces and requirements of my other strings, I have room for 60 characters between the $from and $subject strings.

Any help is appreciated.

Answer

devnull picture devnull · Nov 6, 2013

I'm looking for a way to truncate a line at 80 characters ...

You could pipe the output to cut:

printf ... | cut -c 1-80

If you wanted to ensure that each line isn't more than 80 characters (or wrap lines to fit in specified width), you could use fold:

printf ... | fold -w 80