Sony Mitto
Sony Mitto

Reputation: 145

Extracting match where there's a match below in a set of lines of files

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"

...

Upvotes: 0

Views: 44

Answers (1)

Ed Morton
Ed Morton

Reputation: 203712

$ 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"

Upvotes: 1

Related Questions