我正在构建一个bash脚本,用于访问yelp api v3并使用CURL导出。但是我的结果只返回一行结果。我希望每个“id”都在一个新的行/行上。这是不起作用的代码
curl -X >> worked.csv GET -w 'name\n' \
"https://api.yelp.com/v3/businesses/search?term=meats&location=san-mateo&price=1&sort_by=rating&DESC" \
-H "authorization: " \
-H "cache-control: no-cache" \
-H "postman-token: ab6e8c01-ac37-bc8e-4baf-1eaddd493592"
,结果就像
{"businesses": [{"id": "carrolls-meats-san-bruno-4" "name": "Carroll's Meats" "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/YXSjhk1LjemzLM8ORnH6Ug/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/carrolls-meats-san-bruno-4?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 209 "categories": [{"alias": "delis" "title": "Delis"} {"alias": "sandwiches" "title": "Sandwiches"} {"alias": "salad" "title": "Salad"}] "rating": 4.5 "coordinates": {"latitude": 37.630225 "longitude": -122.412556} "transactions": ["pickup"] "price": "$" "location": {"address1": "189 San Bruno Ave W" "address2": "" "address3": "" "city": "San Bruno" "zip_code": "94066" "country": "US" "state": "CA" "display_address": ["189 San Bruno Ave W" "San Bruno CA 94066"]} "phone": "+16508718593" "display_phone": "(650) 871-8593" "distance": 12007.993205864} {"id": "the-ravioli-house-san-mateo" "name": "The Ravioli House" "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/7N4_sUuCsHjwlMmnXWD1AQ/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/the-ravioli-house-san-mateo?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 392 "categories": [{"alias": "delis" "title": "Delis"} {"alias": "sandwiches" "title": "Sandwiches"} {"alias": "catering" "title": "Caterers"}] "rating": 4.5 "coordinates": {"latitude": 37.5640830993652 "longitude": -122.320419311523} "transactions": [] "price": "$" "location": {"address1": "505 S B St" "address2": "" "address3": "" "city": "San Mateo" "zip_code": "94401" "country": "US" "state": "CA" "display_address": ["505 S B St" "San Mateo CA 94401"]} "phone": "+16503447433" "display_phone": "(650) 344-7433" "distance": 1108.606250918} {"id": "the-sandwich-spot-redwood-city-5" "name": "The Sandwich Spot" "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/Id7DFC0t6bqZnIQr9qP3eQ/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/the-sandwich-spot-redwood-city-5?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 623 "categories": [{"alias": "sandwiches" "title": "Sandwiches"} {"alias": "hookah_bars" "title": "Hookah Bars"} {"alias": "newamerican" "title": "American (New)"}] "rating": 4.5 "coordinates": {"latitude": 37.4864070738747 "longitude": -122.231816384113} "transactions": ["pickup"] "price": "$" "location": {"address1": "2420 Broadway St" "address2": null "address3": "" "city": "Redwood City" "zip_code": "94063" "country": "US" "state": "CA" "display_address": ["2420 Broadway St" "Redwood City CA 94063"]} "phone": "+16502991300" "display_phone": "(650) 299-1300" "distance": 10571.70939848} {"id": "lorenzos-sandwich-shop-belmont" "name": "Lorenzo's Sandwich Shop" "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/AXRPqkJ6v5lCm58xGlvvSA/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/lorenzos-sandwich-shop-belmont?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 343 "categories": [{"alias": "sandwiches" "title": "Sandwiches"}] "rating": 4.5 "coordinates": {"latitude": 37.5127526153161 "longitude": -122.293408720031} "transactions": [] "price": "$" "location": {"address1": "911 Villa Ave" "address2": "" "address3": "" "city": "Belmont" "zip_code": "94002" "country": "US" "state": "CA" "display_address": ["911 Villa Ave" "Belmont CA 94002"]} "phone": "+16505924321" "display_phone": "(650) 592-4321" "distance": 5090.5313565159995} {"id": "takahashi-market-san-mateo" "name": "Takahashi Market" "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/fulLwbq-_82bvo2VZNxTPw/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/takahashi-market-san-mateo?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 505 "categories": [{"alias": "grocery" "title": "Grocery"}] "rating": 4.5 "coordinates": {"latitude": 37.566842 "longitude": -122.320909} "transactions": [] "price": "$" "location": {"address1": "221 S Claremont St" "address2": "" "address3": "" "city": "San Mateo" "zip_code": "94401" "country": "US" "state": "CA" "display_address": ["221 S Claremont St" "San Mateo CA 94401"]} "phone": "+16503430394" "display_phone": "(650) 343-0394" "distance": 1396.4965773659999} {"id": "mid-east-market-san-bruno" "name": "Mid East Market" "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/wAwXB8A_VlHFIsLxqM6AEw/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/mid-east-market-san-bruno?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 119 "categories": [{"alias": "intlgrocery" "title": "International Grocery"}] "rating": 4.5 "coordinates": {"latitude": 37.61506 "longitude": -122.40562} "transactions": ["pickup"] "price": "$" "location": {"address1": "1776 El Camino Real" "address2": "" "address3": "" "city": "San Bruno" "zip_code": "94066" "country": "US" "state": "CA" "display_address": ["1776 El Camino Real" "San Bruno CA 94066"]} "phone": "+16508757100" "display_phone": "(650) 875-7100" "distance": 10439.216137762} {"id": "eat-on-monday-mountain-view-3" "name": "Eat On Monday" "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/eNtpByJ9hwbpbyqWWgNKDA/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/eat-on-monday-mountain-view-3?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 164 "categories": [{"alias": "foodtrucks" "title": "Food Trucks"} {"alias": "catering" "title": "Caterers"}] "rating": 4.5 "coordinates": {"latitude": 37.388427734375 "longitude": -122.075866699219} "transactions": [] "price": "$" "location": {"address1": "" "address2": "" "address3": "" "city": "Mountain View" "zip_code": "94041" "country": "US" "state": "CA" "display_address": ["Mountain View CA 94041"]} "phone": "+14088130288" "display_phone": "(408) 813-0288" "distance": 28227.099396999994} {"id": "grand-leader-market-san-bruno" "name": "Grand Leader Market" "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/IYHet-nbXDzETf1kI8hfGA/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/grand-leader-market-san-bruno?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 90 "categories": [{"alias": "delis" "title": "Delis"} {"alias": "sandwiches" "title": "Sandwiches"} {"alias": "coffee" "title": "Coffee & Tea"}] "rating": 4.5 "coordinates": {"latitude": 37.626651 "longitude": -122.4109783} "transactions": ["pickup"] "price": "$" "location": {"address1": "600 San Mateo Ave" "address2": "" "address3": "" "city": "San Bruno" "zip_code": "94066" "country": "US" "state": "CA" "display_address": ["600 San Mateo Ave" "San Bruno CA 94066"]} "phone": "+16505892928" "display_phone": "(650) 589-2928" "distance": 11634.113107338} {"id": "roma-deli-san-bruno" "name": "Roma Deli" "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/o8Y-YjVp0-oU9iZbnostGA/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/roma-deli-san-bruno?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 247 "categories": [{"alias": "italian" "title": "Italian"} {"alias": "delis" "title": "Delis"} {"alias": "sandwiches" "title": "Sandwiches"}] "rating": 4.5 "coordinates": {"latitude": 37.62214 "longitude": -122.41094} "transactions": [] "price": "$" "location": {"address1": "424 San Mateo Ave" "address2": "" "address3": "" "city": "San Bruno" "zip_code": "94066" "country": "US" "state": "CA" "display_address": ["424 San Mateo Ave" "San Bruno CA 94066"]} "phone": "+16505892360" "display_phone": "(650) 589-2360" "distance": 11295.457920863999} {"id": "little-lucca-sandwich-shop-burlingame" "name": "Little Lucca Sandwich Shop" "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/nVuYW04zFKmF_yr8oGRpPg/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/little-lucca-sandwich-shop-burlingame?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 790 "categories": [{"alias": "sandwiches" "title": "Sandwiches"}] "rating": 4.0 "coordinates": {"latitude": 37.5941590204541 "longitude": -122.384402} "transactions": ["pickup"] "price": "$" "location": {"address1": "Burlingame Shopping Plz" "address2": "" "address3": "1809 El Camino Real" "city": "Burlingame" "zip_code": "94010" "country": "US" "state": "CA" "display_address": ["Burlingame Shopping Plz" "1809 El Camino Real" "Burlingame CA 94010"]} "phone": "+16506978389" "display_phone": "(650) 697-8389" "distance": 7534.49696754} {"id": "gracies-delectables-san-carlos" "name": "Gracie's Delectables" "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/0h6e-kELZgT6r3Kn4uG1dg/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/gracies-delectables-san-carlos?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 166 "categories": [{"alias": "delis" "title": "Delis"} {"alias": "sandwiches" "title": "Sandwiches"}] "rating": 4.5 "coordinates": {"latitude": 37.5023264 "longitude": -122.2563865} "transactions": [] "price": "$" "location": {"address1": "902 Laurel St" "address2": "" "address3": "" "city": "San Carlos" "zip_code": "94070" "country": "US" "state": "CA" "display_address": ["902 Laurel St" "San Carlos CA 94070"]} "phone": "+16505954049" "display_phone": "(650) 595-4049" "distance": 7787.041842369999} {"id": "green-valley-produce-market-san-bruno" "name": "Green Valley Produce Market" "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/IrRa7TLVY7evVWezBmvz1w/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/green-valley-produce-market-san-bruno?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 88 "categories": [{"alias": "markets" "title": "Fruits & Veggies"} {"alias": "grocery" "title": "Grocery"} {"alias": "mediterranean" "title": "Mediterranean"}] "rating": 4.5 "coordinates": {"latitude": 37.6295700073242 "longitude": -122.415550231934} "transactions": ["delivery" "pickup"] "price": "$" "location": {"address1": "504 San Bruno Ave W" "address2": "" "address3": "" "city": "San Bruno" "zip_code": "94066" "country": "US" "state": "CA" "display_address": ["504 San Bruno Ave W" "San Bruno CA 94066"]} "phone": "+16502259027" "display_phone": "(650) 225-9027" "distance": 12152.16818284} {"id": "bonne-sante-burlingame" "name": "Bonne Sante" "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/_XnPmufKiA0Zfu4KwhT0kQ/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/bonne-sante-burlingame?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 535 "categories": [{"alias": "sandwiches" "title": "Sandwiches"} {"alias": "burgers" "title": "Burgers"} {"alias": "tradamerican" "title": "American (Traditional)"}] "rating": 4.0 "coordinates": {"latitude": 37.58661 "longitude": -122.36469} "transactions": [] "price": "$" "location": {"address1": "1184 Broadway" "address2": null "address3": "" "city": "Burlingame" "zip_code": "94012" "country": "US" "state": "CA" "display_address": ["1184 Broadway" "Burlingame CA 94012"]} "phone": "+16503421529" "display_phone": "(650) 342-1529" "distance": 5637.0513114} {"id": "fare-pdq-belmont" "name": "Fare PDQ" "image_url": "https://s3-media4.fl.yelpcdn.com/bphoto/njx8dlNiOpFLiwUxIHY-fQ/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/fare-pdq-belmont?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 471 "categories": [{"alias": "sandwiches" "title": "Sandwiches"} {"alias": "tradamerican" "title": "American (Traditional)"} {"alias": "hotdogs" "title": "Fast Food"}] "rating": 4.0 "coordinates": {"latitude": 37.5164058 "longitude": -122.2713519} "transactions": [] "price": "$" "location": {"address1": "1480 El Camino Real" "address2": "" "address3": "" "city": "Belmont" "zip_code": "94002" "country": "US" "state": "CA" "display_address": ["1480 El Camino Real" "Belmont CA 94002"]} "phone": "+16505953273" "display_phone": "(650) 595-3273" "distance": 5756.736800661999} {"id": "americano-deli-and-grill-san-mateo" "name": "Americano Deli & Grill" "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/BOsNQbxfnOgHXHuhM_7D9Q/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/americano-deli-and-grill-san-mateo?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 262 "categories": [{"alias": "sandwiches" "title": "Sandwiches"} {"alias": "delis" "title": "Delis"}] "rating": 4.0 "coordinates": {"latitude": 37.54097 "longitude": -122.29099} "transactions": ["pickup"] "price": "$" "location": {"address1": "224 E Hillsdale Blvd" "address2": null "address3": "" "city": "San Mateo" "zip_code": "94403" "country": "US" "state": "CA" "display_address": ["224 E Hillsdale Blvd" "San Mateo CA 94403"]} "phone": "+16502120500" "display_phone": "(650) 212-0500" "distance": 2604.71759467} {"id": "chavez-supermarket-and-taqueria-redwood-city-2" "name": "Chavez Supermarket & Taqueria" "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/ArimTcNjSxfyUpOr4m6LcA/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/chavez-supermarket-and-taqueria-redwood-city-2?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 170 "categories": [{"alias": "grocery" "title": "Grocery"} {"alias": "mexican" "title": "Mexican"}] "rating": 4.0 "coordinates": {"latitude": 37.4885509 "longitude": -122.2353577} "transactions": [] "price": "$" "location": {"address1": "775 Arguello St" "address2": "" "address3": "" "city": "Redwood City" "zip_code": "94063" "country": "US" "state": "CA" "display_address": ["775 Arguello St" "Redwood City CA 94063"]} "phone": "+16503678819" "display_phone": "(650) 367-8819" "distance": 10184.08778943} {"id": "leonardos-italian-deli-and-cafe-millbrae" "name": "Leonardo's Italian Deli & Cafe" "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/5SNjXM48EGwYbcM2z7KW7Q/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/leonardos-italian-deli-and-cafe-millbrae?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 160 "categories": [{"alias": "delis" "title": "Delis"} {"alias": "coffee" "title": "Coffee & Tea"} {"alias": "italian" "title": "Italian"}] "rating": 4.0 "coordinates": {"latitude": 37.60319 "longitude": -122.39444} "transactions": [] "price": "$" "location": {"address1": "540 Broadway" "address2": "" "address3": "" "city": "Millbrae" "zip_code": "94030" "country": "US" "state": "CA" "display_address": ["540 Broadway" "Millbrae CA 94030"]} "phone": "+16506979779" "display_phone": "(650) 697-9779" "distance": 8782.386767438} {"id": "ikes-lair-of-redwood-city-redwood-city-2" "name": "Ike's Lair of Redwood City" "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/cQ8HkW_Rd5RYsYp2L8-BOA/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/ikes-lair-of-redwood-city-redwood-city-2?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 543 "categories": [{"alias": "sandwiches" "title": "Sandwiches"} {"alias": "delis" "title": "Delis"}] "rating": 4.0 "coordinates": {"latitude": 37.4861346 "longitude": -122.2342516} "transactions": ["pickup"] "price": "$" "location": {"address1": "2655 Broadway St" "address2": "" "address3": "" "city": "Redwood City" "zip_code": "94063" "country": "US" "state": "CA" "display_address": ["2655 Broadway St" "Redwood City CA 94063"]} "phone": "+16503652200" "display_phone": "(650) 365-2200" "distance": 10447.842361096} {"id": "the-habit-burger-grill-san-mateo" "name": "The Habit Burger Grill" "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/wdJC9EzmfvkrHyLXot_-Kg/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/the-habit-burger-grill-san-mateo?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 446 "categories": [{"alias": "burgers" "title": "Burgers"} {"alias": "sandwiches" "title": "Sandwiches"}] "rating": 4.0 "coordinates": {"latitude": 37.544952 "longitude": -122.291011} "transactions": [] "price": "$" "location": {"address1": "1100 Park Pl" "address2": "Ste 20" "address3": "" "city": "San Mateo" "zip_code": "94403" "country": "US" "state": "CA" "display_address": ["1100 Park Pl" "Ste 20" "San Mateo CA 94403"]} "phone": "+16502869944" "display_phone": "(650) 286-9944" "distance": 2356.3877422339997} {"id": "ikes-place-san-mateo-5" "name": "Ike's Place" "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/b_c_xWsR7t5wZwf5guC_zQ/o.jpg" "is_closed": false "url": "https://www.yelp.com/biz/ikes-place-san-mateo-5?adjust_creative=TRh56Tszp1lUJualdVr-6w&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=TRh56Tszp1lUJualdVr-6w" "review_count": 278 "categories": [{"alias": "sandwiches" "title": "Sandwiches"}] "rating": 4.0 "coordinates": {"latitude": 37.5674163928038 "longitude": -122.31955400788} "transactions": ["pickup"] "price": "$" "location": {"address1": "680 E 3rd Ave" "address2": null "address3": "" "city": "San Mateo" "zip_code": "94401" "country": "US" "state": "CA" "display_address": ["680 E 3rd Ave" "San Mateo CA 94401"]} "phone": "+16503758900" "display_phone": "(650) 375-8900" "distance": 1421.655389586}] "total": 40 "region": {"center": {"latitude": 37.558179800000005 "longitude": -122.3236981420565}}}id
解决此问题的最佳方法是什么?我很新,所以我可能会遗漏一些非常基本的东西。
答案 0 :(得分:1)
应该尝试JSON解析应用程序(json或jq)并将管道卷曲结果发送给它:
curl ... | json businesses | json -a -d, id name >> worked.csv
这将打印所有ID,每行一个。
答案 1 :(得分:0)
感谢@ gonzalo-matheu让我采用不同的方式完成这项工作。
我最终使用sed并剥离它。经过一些精益求精的工作,这很好地结果。这是结束。
curl -X >> worked.csv GET \
"https://api.yelp.com/v3/businesses/search?term=meats&location=san-mateo&price=1&sort_by=rating&DESC" \
-H "authorization: Bearer (AUTH_CODE)" \
-H "cache-control: no-cache" \
-H "postman-token: ab6e8c01-ac37-bc8e-4baf-1eaddd493592"
cat worked.csv | sed -r 's/\<id\>/\n/g;s/://g;s/"//g' > worked.csv