0

I have a big text file. The combination of building and name in one section of file is unique but they may be apart by many lines (variable). How do I get all "name" values for a given "building"? Assume I know how many "name" are there for each "building".

I can do (say there are 7 "name" in the file for this building) this to see 10 lines before and get all the "name" for a "building" but this is not easily parsable for a very big file

cat filename |grep -m 7 -B 10 "building1"

File example below (if format is not correct, use this link) https://docs.google.com/document/d/1HuingqaWvKl677SilRST2pNhmenQZ6-g7AaiO2FRW0Q/edit

name: "Name1"
something1: "ViewMessage1" 
something2: "ViewMessage2"
texttext
texttext
texttext
building: "building1"
name: "Name2"
something1: "ViewMessage1"
something2: "ViewMessage2"
texttext
texttext
texttext
texttext
texttext
texttext
building: "building1"
texttext
texttext
texttext
name: "Name3"
something1: "ViewMessage1"
something2: "ViewMessage2"
building: "building1"
texttext
texttext
texttext
name: "Name4"
something1: "ViewMessage1"
something2: "ViewMessage2"
texttext
texttext
texttext
texttext
building: "building2"

Expected Output

Name1, Name2, Name3: "building1"
Name4: "building2"

...
Sony Mitto
  • 145
  • 1
  • 1
  • 9
  • Does the name always come before the building? – Adam Sep 13 '18 at 16:27
  • That's correct. "name" always comes before the corresponding "building". There may be zero-n sentences in-between – Sony Mitto Sep 13 '18 at 16:51
  • Possible duplicate of [How to find patterns across multiple lines using grep?](https://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep) – Nissa Sep 13 '18 at 17:25
  • @StephenLeppik no, it's not a dup of that and none of the solutions posted for that question would answer this question. – Ed Morton Sep 13 '18 at 20:04

1 Answers1

1
$ awk -F'(: *)?"' '{f[$1]=$2} $1=="building" && $2=="building1"{print f["name"]}' file
Name1
Name2
Name3

$ awk -F'(: *)?"' '
    { f[$1]=$2 }
    $1=="building" { names[$2]=($2 in names ? names[$2] ", " : "") f["name"] }
    END { for (bldg in names) print names[bldg] ": \"" bldg "\"" }
' file
Name1, Name2, Name3: "building1"
Name4: "building2"
Ed Morton
  • 188,023
  • 17
  • 78
  • 185