swburk
swburk

Reputation: 5

Use sed/awk to truncate long email headers

I have a long to field in an email header that spans several lines, like this:

To: John Smith <[email protected]>, Alex Smith <[email protected]>,
    Superman Smith <[email protected]>, Devin Smith <[email protected]>,
    Al Smith <[email protected]>, Jane Smith <[email protected]>,
    Thomas Smith <[email protected]>

I want to truncate it to something shorter, like this:

To: John Smith <[email protected]>, Alex Smith <alexsmith@examp...

Basically, I want the output to be one line, fitting as much as possible within the width of the terminal window (I'm guessing using the $COLUMNS variable).

Upvotes: 0

Views: 261

Answers (2)

shellter
shellter

Reputation: 37298

It's not clear what context your doing this, but given a file that has an email message in it,

 awk -v maxLineSz=${COLUMNS:-80} \
   '/^To:/{if (length() > maxLineSz-4) { $0=substr($0,1,maxLineSz-4) "..." }}1' emailFile 

output

To: John Smith <[email protected]>, Alex Smith <[email protected]>, Supe...

The only lines affected are lines beginning with 'To:'

the 1 at the end, ensures all lines of input are printed.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247012

The formail utility is handy here:

formail -c < ~/tmp/email.eml | 
sed -r 's/\t/ /g; s/^(.{'$(( $(tput cols) - 5))'}).*/\1 .../'

formail can be found in the procmail package

Using tput to query the terminal size

Upvotes: 3

Related Questions