xpisme
xpisme

Reputation: 19

how to deal two intersection file using awk

I have two files. every file has unique job_id value.

shop_file.txt This file, job_id value always is equal to 21.

407486;{"shop_id":"407486","job_id":21}
163181148;{"shop_id":"163181148","job_id":21}
1510942977;{"shop_id":"1510942977","job_id":21}

dish_file.txt This file, job_id value always is equal to 23.

7491777303;{"shop_id":"407486","dish_id":"7491777303","job_id":23}
1667364700;{"shop_id":"1664150969","dish_id":"1667364700","job_id":23}
1932540486;{"shop_id":"1932534033","dish_id":"1932540486","job_id":23}
1932540468;{"shop_id":"1932534033","dish_id":"1932540468","job_id":23}
1932540477;{"shop_id":"1932534033","dish_id":"1932540477","job_id":23}
1932540456;{"shop_id":"1932534033","dish_id":"1932540456","job_id":23}
1673778516;{"shop_id":"1478428493","dish_id":"1673778516","job_id":23}
1673462967;{"shop_id":"1642179256","dish_id":"1673462967","job_id":23}
1721592562;{"shop_id":"1697153440","dish_id":"1721592562","job_id":23}
8491777303;{"shop_id":"407486","dish_id":"8491777303","job_id":23}

same shop_id value must exist two files, how to use awk get this result.

7491777303;{"shop_id":"407486","dish_id":"7491777303","job_id":23}
8491777303;{"shop_id":"407486","dish_id":"8491777303","job_id":23}

Upvotes: 0

Views: 80

Answers (2)

jas
jas

Reputation: 10865

Ideally, JSON data should be parsed as such, but assuming the structure is fixed and consistently as shown (importantly, shop_id being the first key and never any commas in its value):

$ awk -F'[;,]' 'NR==FNR {a[$2]; next} $2 in a' shop_file.txt dish_file.txt
7491777303;{"shop_id":"407486","dish_id":"7491777303","job_id":23}
8491777303;{"shop_id":"407486","dish_id":"8491777303","job_id":23}

Upvotes: 2

Kent
Kent

Reputation: 195229

The below grep and sed one liner should help for the given example:

grep -f <(sed 's/.*\("shop_id"[^,]*\).*/\1/ shopFile) dishFile

Upvotes: 1

Related Questions