[英]How to print character just after a particular character in a string using bash
[英]How to find the unusual character ' using POSIX in bash?
這里有一些名字:
El Peulo'Pasa, 范奧德里斯科爾, Mike_Willam
如何通過命令 find 在 bash 中使用 POSIX 過濾包含 ' 的名稱?
如果我使用以下命令,
find . -maxdepth 1 -mindepth 1 -type d -regex '^.*[']*$' -print
Bash 遇到問題,因為語法 ' 會自動將輸入轉換為字符串
您根本不需要-regex
( 這是一個非 POSIX 操作); -name
。 ( -mindepth
和-maxdepth
也是 POSIX 標准中不存在的擴展)。
要制作'
文字,請將其放在雙引號內,或放在未加引號的上下文中,並在其前面加上反斜杠:
find . -maxdepth 1 -mindepth 1 -type d -name "*'*" -print
...或 100% 相同但難以閱讀的命令行...
find . -maxdepth 1 -mindepth 1 -type d -name '*'\''*' -print
如果您只是搜索當前目錄(而不是其子目錄),您甚至不需要find
,只需要一個通配符(“glob”)表達式:
ls *\'*
(請注意, '
必須轉義或雙引號,但星號不能。)
如果要對這些文件進行操作,可以直接使用該通配符表達式:
dosomethingwith *\'*
# or
for file in *\'*; do
dosomethingwith "$file"
done
...或將文件名存儲在一個數組中,然后使用它。 這涉及使引用恰到好處,以避免文件名中的其他奇怪字符(例如空格)出現問題:
filelist=( *\'* )
dosomethingwith "${filelist[@]}"
# or
for file in "${filelist[@]}"; do
dosomethingwith "$file"
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.