[plug] Sanitising Aptitude output for logging

Alex Nordstrom lx at se.linux.org
Mon Mar 28 13:16:17 WST 2005

Mostly one for the archives, but I'd be interested in more elegant 
solutions as well.

Executive summary: Getting output from Debian's Aptitude sanitised for 
logging is a real pain in the behind.

I'm quite a lazy person, so I like to have cron doing things like 
pulling updates for me while I sleep:

10 6 * * * root apt-get -qq update; echo; apt-get -dqy dist-upgrade

However, since I switched to Aptitude, the output that I receive in my 
mailbox using this cronjob:

10 6 * * * root aptitude update; echo; aptitude -dy dist-upgrade

...is quite horrible. The problem is that Aptitude, like Apt-Get without 
-q or -qq, produces output suitable for screen but not for logging. 
Instead, when run interactively, it prints carriage return characters 
to overwrite the same line several times in order to display progress 

Aptitude does not have a -q or -qq (quiet) option. Since this was 
pointed out in a bug report just one and a half years ago, with a patch 
posted half a year later, it apparently hasn't had the time to get it 
into Sid yet. Grumble.

So Aptitude's output looks thusly (just a representative line):

^MReading Package Lists... 0%^M^MReading Package Lists... 100%^M^MReading Package Lists... Done^M

Hence, I tried piping the lot through the following sed command:

sed 's/.*\r[^$]//g' -

This is of course meant to delete any carriage return character at the 
end of a line and also delete everything before the carriage 
return in question on that line. Only, it takes one character too much, 
so the output becomes:

eading Package Lists... Done^M
uilding Dependency Tree       ^M
eading extended state information       ^M

After a bit of fiddling, I came up with the following solution:

10 6 * * * root ( aptitude update; echo; aptitude -dy dist-upgrade ) 2>&1 | sed 's/\r$//g;s/.*\r//g'

This essentially performs the same manoeuvre in two steps: first it 
strips out any \r that are at the end of a line, and then it deletes 
everything up to and including the last \r on any line.

And that produces nice Aptitude output.

Alex Nordstrom
Please do not CC me in followups; I am subscribed to plug.

More information about the plug mailing list