段落の多次元データの解析

段落の多次元データの解析

PDFレポートのデータを解析し、特定の興味深い要素をフィルタリングしたいと思います。これを使用して、pdftotext -layout始点として次の形式のデータを取得します。

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         

私の予想される出力は次のとおりです。各 " Interesting"は果物とそのレコード番号です。とは別に果物が記録上の最初の場合:

Record   Info
123      lemon
789      lychee
789      passionfruit

現在インスピレーションを受けてこの問題------レコード区切り文字をで置き換え、\n\nを使用してレコードヘッダーを削除しましたsed。これにより、一致するレコードがある段落を見つけることができますawk

awk -v RS='' '/\n   .....................yep/'

{3}.{21}(そのうちの1つawkまたはそれに似ていると書く方法を見つけることは確かに別の日の戦いです:/)

これにより、次のようなきちんとした段落が生成されます。

123      apple          yep         
         orange         nope         
         lemon          yep          

789      kumquat        nope         
         lychee         yep          
         passionfruit   yep          
         yam            nope         

ここでは、次の方法で目的の出力を取得できます。

  • 最初のレコード番号列、または前の行の 2 番目のレコード番号列に入力された 2 番目のレコード番号列を追加します。
  • 最初の列にレコード番号がある行を削除する
  • 興味のない行を削除
  • cut最後の列のうち

私は一般的にここで正しい方向に行きますか、それとも多次元データを解析するより直接的な方法がありますか?おそらくgrep興味深い行(yepレコード番号があるかどうかにかかわらず)をpingしてから、そこにgrep空でないレコード番号がある次の行で逆に作業することができますか?

ベストアンサー1

状況が複雑すぎる可能性があります。

$ 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

awkスクリプトをより垂直にするには、どのように機能するかを説明してください。

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.

おすすめ記事