我有一個檔案(list.txt),其中包含數千行的串列,如下所示:
carpet-redandblue
shelf-brown
metaldesk-none
是否有一個腳本可以用來洗掉“-”之后的所有內容,包括“-”?
這是我到目前為止所擁有的:
set theFile to "/Users/home/Desktop/list.txt"
if theFile contains "-" then
set eol to "-"
else
set eol to "-"
end if
但似乎不起作用。我必須用檔案名和路徑定義輸出檔案嗎?
uj5u.com熱心網友回復:
這應該可以滿足您的需求,至少據我所知。該腳本將文本檔案讀入一個變數。然后它將文本分成段落(或行)并在第一個破折號處分割每一行。然后它將每一行轉換回文本的段落。最后,它將生成的文本寫入文本檔案。如果您更喜歡以其他方式使用生成的文本,則將其存盤在prunedText變數中。
use scripting additions
(*
set rawText to "carpet-redandblue
shelf-brown
metaldesk-none"
*)
set rawText to read file ((path to desktop as text) & "list.txt")
set AppleScript's text item delimiters to "-"
set paraText to paragraphs of rawText
--> {"carpet-redandblue", "shelf-brown", "metaldesk-none"}
set wordPara to {}
repeat with eachLine in paraText
set end of wordPara to first text item of eachLine
end repeat
--> {"carpet", "shelf", "metaldesk"}
set AppleScript's text item delimiters to linefeed
set prunedText to wordPara as text
(*
"carpet
shelf
metaldesk"
*)
-- optionally
tell application "Finder"
set nl to ((path to desktop as text) & "newList.txt") as ?class furl?
end tell
close access (open for access nl)
write prunedText to nl as text
uj5u.com熱心網友回復:
我認為這是最簡單的解決方案。
- 打開終端.app
- 轉到Finder并按住?鍵,將包含“list.txt”檔案的檔案夾直接拖放到終端視窗中。這會將終端中的當前作業目錄更改為包含“list.txt”檔案的檔案夾。
- 然后將以下代碼粘貼到該終端視窗中,然后按回車鍵...
grep -E -o "^\w*" list.txt > new_list.txt
這將創建一個名為“new_list.txt”的新檔案,其中連字符以及它們之后的所有內容都從原始 list.txt 的每一行中洗掉
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517831.html
標籤:苹果系统文本苹果脚本
下一篇:SwiftUI縮放后臺攔截點擊
