我正在尝试为我的文件找到一个特定的值,这里是它的一个快速片段:
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
只会匹配以 ,? 开头的行
答案1
有些人在遇到问题时会想“我知道,我会使用正则表达式。”现在他们有两个问题。-杰米·扎温斯基
你似乎想要的是
grep -E "^([^,]*,){8}[^0]" BC_Liquor_Store_Product_Price_List.csv
那是
- 锚定在行的起始处
^
- 匹配任意数量的非逗号字符,后跟逗号,8 次
- 然后匹配
0
第 9 个字段开头的一个非字符
然而在我看来,这很脆弱,不应该在任何严肃的应用程序中使用它。它很难可靠地匹配数值——例如
尤其是“关于匹配数字的说明(提示:它比你想象的要难)”。请考虑使用类似
awk -F, 'NR==1 || $9+0 > 1.0' BC_Liquor_Store_Product_Price_List.csv
或者
perl -F, -lne 'print if $. == 1 || $F[8] >= 1.0' BC_Liquor_Store_Product_Price_List.csv
或者(更好,因为它可以处理复杂的 CSV 功能,例如引用和嵌入逗号)
mlr --csv filter '$PRODUCT_LITRES_PER_CONTAINER >= 1.0' BC_Liquor_Store_Product_Price_List.csv