我正試圖從一個輸出大量文本行的命令的輸出中提取特定行。在這一點上,我遇到了一點困難:
這就提取了正確的行,但它把它們都放在一個長行中,用空格而不是換行符來分隔匹配。
換句話說,我期待的是這樣的結果: 如果我在終端中對包含 system_profiler SPApplicationsDataType 輸出的文本檔案運行同樣的 egrep 命令,我得到的就是這個結果。但是上面的命令并沒有把它放到pathsText中。相反,它給了我: 不幸的是,我不能僅僅根據空格來分割,因為每一行中包含的路徑可能包含空格。
我做錯了什么? 我在做什么?
uj5u.com熱心網友回復: 仍然無法復制: 編輯: 答案就在這里: https://www.google.com/search?q=macos assign variable looses carriage return of line feed/a> 在stackoverflow.com上找到一個鏈接:
標籤:pathsText=$(system_profiler SPApplicationsDataType | eg_profiler)
pathsText=$(system_profiler SPApplicationsDataType | egrep '^s Location:s .*$')
位置。/a/b
定位。/c/d/e
...
Location: /a/b 位置。/c/d/e ...
luuk@mini ~ % pathsText=$(system_profiler SPApplicationsDataType | egrep '^s Location:s .*$')
luuk@mini ~ % echo $pathsText
位置。/System/Applications/System Preferences.app
位置:/System/Applications/System Preferences.app /System/Applications/Utilities/Terminal.app
位置:/System/Applications/Utilities/Terminal.app /System/Applications/TV.app位置:/System/Applications/TV.app
位置:/System/Applications/FaceTime.app /System/Applications/FaceTime.app
位置:/System/Applications/TextEdit.app /System/Applications/TextEdit.app
位置:/System/Applications/TextEdit.app /System/Applications/Time Machine.app
位置:/System/Applications/App Store.app /System/Applications/App Store.app:/System/Applications/App Store.app
地點:/System/Applications/App Store.app /System/Library/ColorSync/Calibrators/Display Calibrator.app
位置:/System/Library/CoreServices.app /System/Library/CoreServices/P.....
