I'm trying to parse data from a PDF report and filter out certain interesting elements. Using pdftotext -layout I get data in this format as my starting point:Record   Info           Interesting  123      apple          yep                  orange         nope                  lemon          yep          ----------------------------------------------- 456      dragonfruit    yep         cucumber       nope         -----------------------------------------------789      kumquat        nope                  lychee         yep                   passionfruit   yep                   yam            nope         -----------------------------------------------987      grapefruit     nope         My intended output is this - every 'Interesting' fruit and its record number except when the fruit is the first fruit in its record:Record   Info123      lemon789      lychee789      passionfruitCurrently, inspired by this question, I'm replacing the ------ record delimiters with \n\n and stripping out the record headers using sed. Then I can find paragraphs with matching records with awk:awk -v RS='' '/\n   .....................yep/'(Figuring out how to write {3}.{21} or similar with one of the awks is definitely a battle for another day :/ )This produces the cleaned-up paragraphs like so:123      apple          yep                  orange         nope                  lemon          yep          789      kumquat        nope                  lychee         yep                   passionfruit   yep                   yam            nope         From here I could get the desired output by:adding a second record number column, populated from the first record number column or the previous row's second record number columndelete rows which have a record number in the first columndelete rows which aren't intererestingcut out the final columnsAm I going broadly in the right direction here, or is there a more straightforward way to parse multidimensional data? Perhaps by grepping an interesting row (has yep and no record number), then grep backwards from there to the next row with a nonblank record number?
Parsing multidimensional data in paragraphs
awk;grep;regular expression
You might be overcomplicating things:$ cat inputRecord   Info           Interesting123      apple          yep         orange         nope         lemon          yep-----------------------------------------------456      dragonfruit    yep         cucumber       nope-----------------------------------------------789      kumquat        nope         lychee         yep         passionfruit   yep         yam            nope-----------------------------------------------987      grapefruit     nope$ awk 'BEGIN {OFS=\t; print Record,Info} NF==3 && NR!=1 { number=$1 } NF!=3 && $2 ~ /yep/ {print number,$1}' inputRecord  Info123     lemon789     lychee789     passionfruitTo make the awk script a little more veritcal, for to explain how it works:BEGIN {                    # This block executes before any data   OFS=\t;               # are parsed, and simply prints a header.   print Record,Info}NF==3 && NR!=1 {           # This block will run on any row (line)   number=$1               # with three fields other than the first}NF!=3 && $2 ~ /yep/ {      # On rows with three fields where the second   print number,$1         # matches the regex /yup/, print the number}                          # grabbed before, and the fruit.