Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm running a cron job which should get only the last result of iostat -d 1 2. This requires some parsing: What is the simplest way to get either the last set of non-empty lines from standard input to standard output if the length of each set is unknown?

Bad/non-working solutions:

  • tail since I'd still need to count the number of lines in the last set. iostat -d 1 2 | tail -$(echo "$(iostat -d 1 2 | wc -l) / 2" | bc) depends on equal set sizes.
  • split/csplit since they output to file, and keep the useless part of the data.
  • iostat -d 1 2 | sed '1,/^$/d' | sed '1,/^$/d' works only in this special case, since it gets the third set of non-empty lines, but also includes any trailing newlines.
  • iostat -d 1 2 | tac | sed '1,/^$/d' | sed '/^$/q' is a slightly better hack: Reverse and print the first set. However, since iostat outputs an empty line at the end, we first remove that then print until the next empty line in the reversed output. Other commands might output any number of newlines at the end, so it's not a general solution. Reverse again if you want to keep the original sequence.
  • grep -P with \Z seems to only detect EOL, not EOF.
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You can use awk with a record separator of "\n\n". That way each "set" is one record and you can easily print out the last one.

iostat -d 1 2 | awk 'BEGIN{RS="\n\n"}END{print}'
share|improve this answer
    
+1, Just Works(TM). To avoid problems with more empty lines, maybe RS="\n\n+" would be useful? –  l0b0 Mar 17 '11 at 10:53
3  
more efficient to put the RS assignment in the BEGIN block. –  glenn jackman Mar 17 '11 at 10:56
    
Better make that awk -vRS= 'END{print}'. First, because the "\n\n" version will print an empty record if there are more than two blank lines at the end of the input. Second, because some versions of awk don't allow RS to be more than one character. An empty RS means “paragraph mode” where any sequence of two or more newlines separate records, and that's required by POSIX. –  Gilles Mar 17 '11 at 20:23
add comment

perl -00 is a good way to read one paragraph at a time, so the last paragraph is:

perl -00 -ne '$para = $_; END {print $para}'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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