1

I have the following output:

Filename:xxxxxxxx
date: 2014/12/24 14:46:41;  author: xxxxx;  state: Exp;  lines: +210 -206
date: 2014/11/18 21:00:43;  author: xxxxx;  state: Exp;  lines: +617 -594
revision 1.23

If i see the 2 date: in the output I want to remove that entire line from the output. 2nd line is there if the dev actually added a bug number and explanation, but in many older checkins they didn't so it grabs the date from the checkin before that. I'd like to have awk remove any 2nd occurance of date: along with the entire line of text behind it.

so im doing a grep date|awk 'FNR <3' to get the text i want, but i'd like to either punt it thru awk again to scrub the 2nd date line out or combine it into one awk command if at all possible.

thanks!

  • Are more than two date lines possible? – A.B. Oct 26 '15 at 20:56
0

Using awk and an input file with the name foo

awk '! /^date/ {print} /^date/ && ! a {a=1; print}' foo

Output

Filename:xxxxxxxx
date: 2014/12/24 14:46:41;  author: xxxxx;  state: Exp;  lines: +210 -206
revision 1.23

If you have more than one block of Filenamerevision in one file, e.g.

Filename:xxxxxxxx
date: 2014/12/24 14:46:41;  author: xxxxx;  state: Exp;  lines: +210 -206
date: 2014/11/18 21:00:43;  author: xxxxx;  state: Exp;  lines: +617 -594
revision 1.23
Filename:xxxxxxxx
date: 2014/12/24 14:46:41;  author: xxxxx;  state: Exp;  lines: +210 -206
date: 2014/11/18 21:00:43;  author: xxxxx;  state: Exp;  lines: +617 -594
revision 1.23

you should use

awk '! /^date/ {a=0; print} /^date/ && ! a {a=1; print}' foo

to get

Filename:xxxxxxxx
date: 2014/12/24 14:46:41;  author: xxxxx;  state: Exp;  lines: +210 -206
revision 1.23
Filename:xxxxxxxx
date: 2014/12/24 14:46:41;  author: xxxxx;  state: Exp;  lines: +210 -206
revision 1.23
0

I know the question asks specifically for an AWK solution, however here's an alternative using Perl:

perl -ne 'if(/^date/){$x ne 0?$x=0:next}print' in
  • if(/^date/){[...]}: if the current line starts with a date string, executes the statements contained in [...];
  • $x ne 0?$x=0:next: if x is not equal to 0, sets x to 0; otherwise moves to the next line;
  • print: prints the current line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.