Home > computers > linux > sed > sedranges > sedrangesexclusive | About

sed '/RE1/,/RE2/!d;//d'

Breaking it down :

  1. sed '/RE1/,/RE2/!d;' will delete everything except what's between RE1 and RE2
  2. // will be replaced with last REs.
  3. d will then remove the matching REs.

Here's an example :

root@messagerie-secours[10.10.10.20] ~ # apt-get -u upgrade --assume-no
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  base-files bind9-host binutils ca-certificates dnsutils dovecot-core dovecot-imapd dovecot-lmtpd dovecot-managesieved dovecot-mysql dovecot-pop3d dovecot-sieve eject git git-man host initramfs-tools libbind9-90 libc-bin libc-dev-bin libc6 libc6-dev libdns-export100
  libdns100 libfreetype6 libgnutls-deb0-28 libgnutls-openssl27 libicu52 libirs-export91 libisc-export95 libisc95 libisccc90 libisccfg-export90 libisccfg90 libjasper1 libldap-2.4-2 liblwres90 libmysqlclient18 librtmp1 libsystemd0 libtasn1-6 libtiff5 libtirpc1 libudev1
  linux-image-3.16.0-4-amd64 linux-libc-dev locales login multiarch-support mysql-client-5.5 mysql-common mysql-server mysql-server-5.5 mysql-server-core-5.5 passwd perl perl-base perl-modules postfix postfix-mysql rpcbind systemd systemd-sysv tzdata udev vim-common
  vim-tiny wget
68 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 95.6 MB of archives.
After this operation, 986 kB disk space will be freed.
Do you want to continue? [Y/n] N
Abort.

We only want the packet names

  base-files bind9-host binutils ca-certificates dnsutils dovecot-core dovecot-imapd dovecot-lmtpd dovecot-managesieved dovecot-mysql dovecot-pop3d dovecot-sieve eject git git-man host initramfs-tools libbind9-90 libc-bin libc-dev-bin libc6 libc6-dev libdns-export100
  libdns100 libfreetype6 libgnutls-deb0-28 libgnutls-openssl27 libicu52 libirs-export91 libisc-export95 libisc95 libisccc90 libisccfg-export90 libisccfg90 libjasper1 libldap-2.4-2 liblwres90 libmysqlclient18 librtmp1 libsystemd0 libtasn1-6 libtiff5 libtirpc1 libudev1
  linux-image-3.16.0-4-amd64 linux-libc-dev locales login multiarch-support mysql-client-5.5 mysql-common mysql-server mysql-server-5.5 mysql-server-core-5.5 passwd perl perl-base perl-modules postfix postfix-mysql rpcbind systemd systemd-sysv tzdata udev vim-common

This is first iteration with sed '/RE1/,/RE2/!d'

root@messagerie-secours[10.10.10.20] ~ # apt-get -u upgrade --assume-no | sed '/The following/,/upgraded/!d'
The following packages will be upgraded:
  base-files bind9-host binutils ca-certificates dnsutils dovecot-core
  dovecot-imapd dovecot-lmtpd dovecot-managesieved dovecot-mysql dovecot-pop3d
  dovecot-sieve eject git git-man host initramfs-tools libbind9-90 libc-bin
  libc-dev-bin libc6 libc6-dev libdns-export100 libdns100 libfreetype6
  libgnutls-deb0-28 libgnutls-openssl27 libicu52 libirs-export91
  libisc-export95 libisc95 libisccc90 libisccfg-export90 libisccfg90
  libjasper1 libldap-2.4-2 liblwres90 libmysqlclient18 librtmp1 libsystemd0
  libtasn1-6 libtiff5 libtirpc1 libudev1 linux-image-3.16.0-4-amd64
  linux-libc-dev locales login multiarch-support mysql-client-5.5 mysql-common
  mysql-server mysql-server-5.5 mysql-server-core-5.5 passwd perl perl-base
  perl-modules postfix postfix-mysql rpcbind systemd systemd-sysv tzdata udev
  vim-common vim-tiny wget
68 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

We're almost good except we don't want the first and last line. This is where we add ;//d, like so :

root@messagerie-secours[10.10.10.20] ~ # apt-get -u upgrade --assume-no | sed '/The following/,/upgraded/!d;//d'
  base-files bind9-host binutils ca-certificates dnsutils dovecot-core
  dovecot-imapd dovecot-lmtpd dovecot-managesieved dovecot-mysql dovecot-pop3d
  dovecot-sieve eject git git-man host initramfs-tools libbind9-90 libc-bin
  libc-dev-bin libc6 libc6-dev libdns-export100 libdns100 libfreetype6
  libgnutls-deb0-28 libgnutls-openssl27 libicu52 libirs-export91
  libisc-export95 libisc95 libisccc90 libisccfg-export90 libisccfg90
  libjasper1 libldap-2.4-2 liblwres90 libmysqlclient18 librtmp1 libsystemd0
  libtasn1-6 libtiff5 libtirpc1 libudev1 linux-image-3.16.0-4-amd64
  linux-libc-dev locales login multiarch-support mysql-client-5.5 mysql-common
  mysql-server mysql-server-5.5 mysql-server-core-5.5 passwd perl perl-base
  perl-modules postfix postfix-mysql rpcbind systemd systemd-sysv tzdata udev
  vim-common vim-tiny wget
root@messagerie-secours[10.10.10.20] ~ # 

If that doesn't work for you here's a bit more complicated version http://sed.sourceforge.net/sedfaq4.html#s4.24


contact : @ychaouche yacinechaouche at yahoocom


QR Code
QR Code sedrangesexclusive (generated for current page)