1

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?

con-f-use
  • 18,813
Insperatus
  • 4,693
  • Why not just use aptitude to bring up a list and go through and flag thse ou want removed? – StarNamer Aug 15 '12 at 18:13
  • I can install aptitude and try that as you've suggested, but will that not be tedious? For instance, is there a history I can search and list only those packages installed on a particular day? If so, could I 'select all' and then remove them in one fell swoop? – Insperatus Aug 15 '12 at 18:44
  • have a look here: https://askubuntu.com/questions/247549/is-it-possible-to-undo-an-apt-get-install-command/1162891#1162891 – T.K Dec 09 '19 at 21:08

2 Answers2

4

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.

con-f-use
  • 18,813
  • That did it! Thank you!! I had come across the greediness setting in my searches and tried to use it with sed ha! – Insperatus Aug 15 '12 at 20:04
  • Note to anyone else trying this: I first pasted relevant portion from my /var/log/apt-history.log into a new file and ran the perl one-liner on THAT rather than my original log file. Then I just cut and pasted that onto the end of sudo apt-get remove in the cli. I'm sure there is a better way to to do this in the terminal via cat and using a pipe or some other cli-fu I'm not yet aware of. I'd want to cat the file and pipe it to the end of my apt-get remove or something...ahh, one day I'll be there. – Insperatus Aug 15 '12 at 20:25
0

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

  • The following packages will be REMOVED: libgconfmm-2.6-1c2 libglademm-2.4-1c2a libgtkmm-2.4-1c2a

    This seems like I'll still be left with lots 'o packages I don't want. Or will the rest of them be dependent on these few and another autoremove would get the rest?

    – Insperatus Aug 15 '12 at 18:45
  • I think the problem is that upon install, ubuntu-desktop calls for many additional packages to be installed - but they aren't necessarily dependent on ubuntu-desktop and so they weren't removed when I uninstalled ubuntu-desktop. Nor do they become broken dependencies when ubuntu-desktop is uninstalled - hence apt-get auroremove doesn't touch them. I'm fairly ignorant though so, that must be taken into account. – Insperatus Aug 15 '12 at 18:54
  • Try running apt-get check first. Though you may be correct that it's not seeing them as broken dependencies. – Zach Sheffler Aug 15 '12 at 19:02
  • That didn't do it either, they must not be broken dependencies. Thanks for your time :) – Insperatus Aug 15 '12 at 20:07