我正在尝试为我的文件找到一个特定的值,这是一个快速的片段:
PRODUCT_TYPE_NAME,PRODUCT_CLASS_NAME,PRODUCT_SUB_CLASS_NAME,PRODUCT_MINOR_CLASS_NAME,PRODUCT_COUNTRY_ORIGIN_NAME,PRODUCT_SKU_NO,PRODUCT_LONG_NAME,PRODUCT_BASE_UPC_NO,PRODUCT_LITRES_PER_CONTAINER,PRD_CONTAINER_PER_SELL_UNIT,PRODUCT_ALCOHOL_PERCENT,CURRENT_DISPLAY_PRICE,SWEETNESS_CODE
LIQUOR,WINE,TABLE WINE,TABLE WINE RED,CANADA,198267,COPPER MOON - MALBEC,48162013513,3,1,14,30.99,0
LIQUOR,WINE,TABLE WINE,TABLE WINE WHITE,CANADA,305375,DOMAINE D'OR - DRY,48162001886,4,1,11.5,32.99,0
LIQUOR,WINE,TABLE WINE,TABLE WINE RED,CANADA,53017,SOMMET ROUGE,58976055050,4,1,12,29.99,0
LIQUOR,WINE,TABLE WINE,TABLE WINE WHITE,CANADA,215525,MISSION RIDGE - PREMIUM DRY WHITE,779646155251,4,1,11,33.99,1
LIQUOR,WINE,TABLE WINE,TABLE WINE RED,UNITED STATES OF AMERICA,168971,ZINFANDEL - BIG HOUSE CARDINAL ZIN,81308001456,3,1,13.5,36.99,0
LIQUOR,WINE,TABLE WINE,TABLE WINE RED,FRANCE,234559,LE VILLAGEOIS RED - CELLIERS LA SALLE,63657001448,4,1,11,34.99,0
LIQUOR,WINE,TABLE WINE,TABLE WINE RED,CANADA,492314,SAWMILL CREEK - MERLOT,63657004074,16,1,12.5,119,0
LIQUOR,WINE,TABLE WINE,TABLE WINE WHITE,CANADA,587584,SOLA,63657006566,4,1,12,32.99,0
LIQUOR,WINE,TABLE WINE,TABLE WINE WHITE,CANADA,100925,GANTON & LARSEN PROSPECT - PINOT BLANC BIRCH CANOE 2011,776545400000,0.75,1,11.5,13.99,0
LIQUOR,SPIRITS,IRISH WHISKY,IRISH WHISKY,IRELAND,10157,JAMESON - IRISH,80432500170,0.75,1,40,34.99,NA
LIQUOR,WINE,TABLE WINE,TABLE WINE WHITE,ITALY,102764,PINOT GRIGIO DELLE VENEZIE - RUFFINO LUMINA,8001660197156,0.75,1,12.5,15.99,0
LIQUOR,SPIRITS,AMERICAN WHISKY,AMERICAN WHISKY,UNITED STATES OF AMERICA,103747,MAKER'S MARK - KENTUCKY BOURBON,85246139431,0.75,1,45,44.95,NA
LIQUOR,SPIRITS,GIN,DRY GIN,CANADA,1040,GORDONS - LONDON DRY,622153139040,0.75,1,40,24.49,NA
LIQUOR,WINE,TABLE WINE,TABLE WINE WHITE,CANADA,104679,CALONA - ARTIST SERIES RESERVE PINOT GRIS 2011/13,58976501656,0.75,1,13.5,12.99,0
LIQUOR,WINE,TABLE WINE,TABLE WINE RED,UNITED STATES OF AMERICA,106476,PINOT NOIR - SIDURI RUSSIAN RIVER 11/12,626990184140,0.75,1,14.5,49.99,0
LIQUOR,SPIRITS,CACHACA,CACHACA,BRAZIL,107029,CACHACA 61,7896547500676,0.7,1,40,28.95,2
LIQUOR,WINE,TABLE WINE,TABLE WINE RED,FRANCE,109082,CHATEAU PAVIE DECESSE 2008,,0.75,1,13,239,0
LIQUOR,SPIRITS,SCOTCH WHISKY,SCOTCH - BLEND,UNITED KINGDOM,1099,JOHNNIE WALKER - RED LABEL,622153631049,0.75,1,40,29.99,NA
LIQUOR,WINE,TABLE WINE,TABLE WINE RED,ITALY,110460,LE CONTRADE - CO.PRO.VI,8004753004010,1,1,12,9.9,0
LIQUOR,SPIRITS,RUM,DARK,CANADA,112433,BACARDI - BLACK,620213055408,0.75,1,40,23.75,NA
LIQUOR,WINE,APERITIF DESSERT AND FORTIFIED WINE,MONTILLA,SPAIN,112789,ALVEAR - MEDIUM DRY,766238303374,0.75,1,17,17.99,3
LIQUOR,SPIRITS,SCOTCH WHISKY,SCOTCH - BLEND,UNITED KINGDOM,112896,JOHNNIE WALKER - RED LABEL,622153631070,1.75,1,40,68.99,NA
现在我需要使用 grep,如果解决方案不涉及 sed、perl、awk 或循环,我会更喜欢。我试过了:
grep -E "^.*(,.*){9}[^0]+" BC_Liquor_Store_Product_Price_List.csv
但这显然得到了一切。我需要获取所有具有 PRODUCT_LITRES_PER_CONTAINER >= 1 的行,但我无法弄清楚如何。.* 得到一切,但在 之前,有一些词,所以我不能这样做:
grep -E "^(,.*){9}[^0]+" BC_Liquor_Store_Product_Price_List.csv
只会匹配以 ,? 开头的行
请参阅“现在您有两个问题”是什么意思?
你似乎在抓住的是
那是
^
0
然后在第 9 个字段的开头匹配一个非字符但是恕我直言,这是脆弱的,不应在任何严肃的应用程序中使用。可靠地匹配数值特别困难 - 参见例如
特别是“关于匹配数字的说明(提示:这比你想象的要难)”部分。请考虑改用类似的东西
或者
或(更好,因为它将处理复杂的 CSV 功能,例如引用和嵌入逗号)