Quantcast
Viewing latest article 1
Browse Latest Browse All 2

Answer by DopeGhoti for Parsing multidimensional data in paragraphs

You might be overcomplicating things:

$ cat input
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
$ awk 'BEGIN {OFS="\t"; print "Record","Info"} NF==3 && NR!=1 { number=$1 } NF!=3 && $2 ~ /yep/ {print number,$1}' input
Record  Info
123     lemon
789     lychee
789     passionfruit

To 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.

Viewing latest article 1
Browse Latest Browse All 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>