Ubuntu: How to use sed to edit /var/log/apt/history.log in order to apt-get remove numerous installed packages?


After running these two commands:

sudo apt-get install ubuntu-desktop  sudo apt-get remove ubuntu-desktop  

I am left with a glut of installed packages that I don't need/want on my Lubuntu install. I just wanted to see how Unity performed under Lubuntu.

I've discovered I can view the list of installed programs by looking at my /var/log/apt/history.log

I'd like to simply cut and paste this list into a sudo apt-get remove command but first I need to delete the portions apt-get won't understand. Basically everything inside the parenthesis (including parenthesis themselves) needs to go. Additionally the following comma and space after each entry will also need deleting.

The closest I've come with sed so far is:

sed -e 's/(.*),[[:space:]]//g' apt-log.txt  

This is not right since it deletes almost everything. I presume this is because its matching the very last instance of "), " and deleting everything before it until "(". Here's a link to program list from the log I'm trying to edit.

As you can imagine, I would rather not edit that list by hand. I know next to nothing about regular expressions and am trying to learn but a few specific pointers would go a long way. Or maybe there's a much better way to go about this?


Your problem with the regular expression is its greediness (the * and + quantifiers try to match as much, as they can). Unfortunately there is no way to tell sed to use non-greedy regular expressions. You have to use Perl to accomplish that, although ugly workarounds for sed do exist.

So try the following Perl one-liner:

perl -pe 's/\(.*?\)(, )?//g' /var/log/apt-history.log  

That should manipulate the text as you wish. You will find PCRE (Perl regular expressions) very similar to the POSIX / GNU BRE and ERE one sed uses.


You're probably better off running apt-get autoremove - which removes broken dependencies - rather than editing a file and passing them on manually.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »