我有一個 CSV 檔案,大約 5k 行,示例如下:
apple,tea,salt,fish
apple,oranges,ketchup
...
salad,oreo,lemon
salad,soda,water
我只需要提取匹配蘋果或沙拉的第一行,并跳過出現這些單詞的其他行。
我可以用正則運算式“apple|salad”做這樣的事情,但它會提取找到這些單詞的所有行。
期望的結果是:
apple,tea,salt,fish
salad,oreo,lemon
我可以在文本編輯器和 OpenOffice Calc 應用程式中使用 REGEX。
uj5u.com熱心網友回復:
你可以使用偉大的
截圖(之后):

uj5u.com熱心網友回復:
在 Notepad 中反復做一個正則運算式替換^(\w ,)(.*)\R\1.*$with \1\2。選擇“環繞”。
解釋:
^ Match beginning of line
(\w ,) Match the leading word plus comma, save to capture group 1
(.*) Match the rest of the line, save to capture group 2
\R Match a line break
\1 Match the same leading word plus comma
.* Match the rest of the line
$ Match the end of the line
替換字串只保留第一行,第二行被丟棄。
示范:
從...開始:
apple,tea1,salt1,fish1
apple,tea2,salt2,fish2
apple,oranges1,ketchup1
apple,oranges2,ketchup2
apple,oranges3,ketchup3
apple,oranges4,ketchup4
salad,oreo1,lemon1
salad,oreo2,lemon2
salad,soda1,water1
salad,soda2,water2
使用上述運算式執行“全部替換”會產生:
apple,tea1,salt1,fish1
apple,oranges1,ketchup1
apple,oranges3,ketchup3
salad,oreo1,lemon1
salad,soda1,water1
再點擊兩次“全部替換”產量:
apple,tea1,salt1,fish1
salad,oreo1,lemon1
每按一次“全部替換”都會洗掉大約一半不需要的行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515564.html
下一篇:使用awk腳本檔案對齊列
