I want to extract name of removed packages from here "cat /var/log/dpkg.log | grep 'remove'"
2013-09-09 15:57:34 remove activity-log-manager:i386 0.9.4-0ubuntu6.2 <none>
2013-09-09 15:57:35 remove activity-log-manager-control-center:i386 0.9.4-0ubuntu6.2 <none>
2013-09-09 15:57:38 remove alacarte:all 3.6.1-0ubuntu3 <none>
2013-09-09 15:57:41 remove deepin-software-center:all 2.1.2.1~precise~NoobsLab.com <none>
I want to grab only name of the packages between remove and colon after package name. I am not a regex expert, I made a regex expression that seems to do the job but when I want to apply it with grep nothing happens. Here is working regex pattern in regex evaluators
(?<=remove)(.*?)(?=:)
But this is not working :
cat /var/log/dpkg.log | grep 'remove' | grep '(?<=remove)(.*?)(?=:)'
What am I missing here?
There's a common core of regular expression syntax however there are distinct flavors. Your expression appears to contain some features specific to the perl flavor, in particular the use of complex lookaround assertions describing the start and end of the pattern to be matched, whereas grep defaults to a basic regular expression (BRE) syntax that only supports a simpler set of these zero-length matches such as line- (
^
,$
) and word-anchors (\>
,\<
).You can enable perl-compatible regular expression (PCRE) support in grep using the -P command line switch (although note that the man page currently describes it as "experimental"). In your case you probably want the -o switch as well to only print the matching pattern, rather than the whole line i.e.
Be aware that this expression may fail if it encounters packages that do not have the :i386 suffix since it may read ahead to a matching colon in the next word, e.g.
You may wish to look at awk instead e.g.
As well as BRE and PCRE, Gnu grep has a further mode called extended regular expression (ERE), specified by the -E command line switch. The man page notes that
However you should note that "no difference in available functionality" does not mean that the syntax is the same. For example, in BRE the
+
character is normally treated as literal, and only becomes a modifier meaning 'one or more instance of the preceding regular expression' if escaped, i.e.whereas for ERE it is exactly the opposite
A similar distinction applies for
sed
invoked without and with the-r
switch.From the
grep
man page:As far as I know,
grep
has no ability to edit the lines that it matches; I would usesed
or possiblytr
for that. Any of the following should get what you want:I'm honestly not sure what your
(?<=remove)(.*?)(?=:)
is trying to accomplish. In regex, brackets are used to define capture groups: you can see that I've used them in the sed commands here -- in there, everything matched will be replaced by the contents of the capture group/1
, the first group to be defined.