5

I have the following text:

Start-Date: 2013-11-11  07:43:49
Commandline: apt-get install --no-install-recommends catfish

Start-Date: 2013-11-11  11:27:07

Start-Date: 2013-10-16  22:53:02

Start-Date: 2013-10-16  22:55:16

Start-Date: 2013-10-17  22:41:09

Start-Date: 2013-10-17  22:42:02

Start-Date: 2013-10-17  22:42:33

Start-Date: 2013-10-17  22:46:01

Start-Date: 2013-10-17  23:00:06
Commandline: apt-get install shimmer-themes

Start-Date: 2013-10-17  23:01:52
Commandline: apt-get install --no-install-recommends gedit

I would like to delete lines beginning with "Start-Date" and the following blank line. In other words, I would like to have just:

Start-Date: 2013-11-11  07:43:49
Commandline: apt-get install --no-install-recommends catfish

Start-Date: 2013-10-17  23:00:06
Commandline: apt-get install shimmer-themes

Start-Date: 2013-10-17  23:01:52
Commandline: apt-get install --no-install-recommends gedit

I hope my question isn't a duplicate of Remove line containing certain string and the following line

3 Answers3

7
sed '/^Start-Date:/ {N; /\n$/d}'

sed's a Stream Editor, slurp up/identify line gaggles and have your way with them.

N is "append a newline and the next input line to the current buffer", so \n in the buffer is the start of an appended line /\n$/d means "if the last appended line in the buffer is empty just drop all of it".

jthill
  • 2,710
1

awk could help:

$ awk '{if ($1=="Commandline:") {printf "%s\n%s\n\n", l,$0}; l=$0}' test.txt 
Start-Date: 2013-11-11  07:43:49
Commandline: apt-get install --no-install-recommends catfish

Start-Date: 2013-10-17  23:00:06
Commandline: apt-get install shimmer-themes

Start-Date: 2013-10-17  23:01:52
Commandline: apt-get install --no-install-recommends gedit
dchirikov
  • 3,888
0

In Perl:

$ perl -ne 'print $last,"$_\n" if /^Commandline/; $last=$_' foo.txt 
Start-Date: 2013-11-11  07:43:49
Commandline: apt-get install --no-install-recommends catfish

Start-Date: 2013-10-17  23:00:06
Commandline: apt-get install shimmer-themes

Start-Date: 2013-10-17  23:01:52
Commandline: apt-get install --no-install-recommends gedit

Or, to remove blank lines:

$ perl -ne 'print $last,$_ if /^Commandline/; $last=$_' foo 
Start-Date: 2013-11-11  07:43:49
Commandline: apt-get install --no-install-recommends catfish
Start-Date: 2013-10-17  23:00:06
Commandline: apt-get install shimmer-themes
Start-Date: 2013-10-17  23:01:52
Commandline: apt-get install --no-install-recommends gedit

The -n switch causes perl to process an input file line by line, saving each line in the special variable $_. The scriptlet above prints the current line ($_) and the previous line ($last) if the current one starts with Commandline. It then saves the current line as $last and moves to the next line.

terdon
  • 242,166
  • 3
    For the record, this can be golfed with "paragraph mode": perl -n00le 'print unless /^Start-Date.*\z/m' file With the side effect of adding an extra blank line at the end of the file. – Joseph R. Nov 11 '13 at 17:03