Search code examples
bashawkemacsterminalorg-mode

How can I output and format the latest entry in an org file to the terminal?


I keep daily notes using emacs and an org file which is structured as follows:

#+SEQ_TODO: TODO(t) IN_PROGRESS(p) BLOCKED(b) ON_HOLD(h) OPEN(o) QA(q) | DONE(d) CANCELLED(c)

* Notes <2019-04-19 Fri>
** To do:
*** Item 1
*** Item 2
* Notes <2019-04-18 Thu>
** To do:
*** Item 1
*** Item 2
*** Item 3
*** Item 4
etc.

I'd like to output the latest entry in the terminal (possibly through grep, cat, sed, or other means), so for example if I type todo in the terminal it would output:

* Notes <2019-04-19 Fri>
** To do:
*** Item 1
*** Item 2

I've tried things like grep -A 5 -i "2019-04-18" ~/Documentes/notes.org but don't want to have to manually specify how many lines to output (the list could be 2 lines or 10 lines). I've also experimented a bit with sed and trying to output between words but haven't had much luck.


Solution

  • Assuming latest entry is always at the top:

    $ awk '(c+=/^\* Notes/)>1{exit} c' file
    * Notes <2019-04-19 Fri>
    ** To do:
    *** Item 1
    *** Item 2
    

    For each line in file; if the line matches ^\* Notes, increase c by 1; if c is greater than 1, exit; if c is greater than zero, print the line.

    And, if a single asterisk at the beginning followed by a space only comes before Notes you can drop Notes from the regexp:

    awk '(c+=/^\* /)>1{exit} c' file