Search code examples
bashawklatexbibtex

Extract bibtex entries based on the year


Okay, I got the file.bib file with multiple entries such

@Book{Anley:2007:shellcoders-handbook-2nd-ed,
  author =   {Chris Anley and John Heasman and Felix Lindner and Gerardo
    Richarte},
  title =    "{The Shellcoder's Handbook}",
  publisher =    {Wiley},
  year =     2007,
  edition =      2,
  month =    aug,
}

there you can find the "year = 2007" line. My task is to filter out the years which are greater than 2020 ($currentyear) or lower than 1900 ($minyear), the result should be a also the output of the month "may", which stands behind a "year" line in this file. (Which is a mistake by the admin). (btw the file is over 4000 lines long).


Solution

  • It is better to use awk for this. Similar to your line, it would read:

    awk -v t1="1900" -v t2="$(date "+%Y")" \
        '!match($0,/year.*=.*/){next}
         {t=substr(RSTART,RLENGTH)
          match(t,/[0-9][0-9][0-9][0-9]/)
          y=substr(RSTART,RLENGTH)
         }
         (y > t1) && (y <= t2) { print y }' file