简体   繁体   English

如何在bash中使用POSIX找到不寻常的字符'?

[英]How to find the unusual character ' using POSIX in bash?

here are some names:这里有一些名字:

El Peulo'Pasa, Van O'Driscoll, Mike_Willam El Peulo'Pasa, 范奥德里斯科尔, Mike_Willam

how to filter the name contains ', using POSIX in bash by command find?如何通过命令 find 在 bash 中使用 POSIX 过滤包含 ' 的名称?

if I use the following command,如果我使用以下命令,

find . -maxdepth 1 -mindepth 1 -type d -regex '^.*[']*$' -print

Bash runs into a problem because the syntax ' will automatically convert the input to string Bash 遇到问题,因为语法 ' 会自动将输入转换为字符串

You don't need -regex ( which is a non-POSIX action ) for this at all;您根本不需要-regex这是一个非 POSIX 操作); -name is more than adequate. -name ( -mindepth and -maxdepth are also extensions that aren't present in the POSIX standard). -mindepth-maxdepth也是 POSIX 标准中不存在的扩展)。

To make a ' literal, put it inside double quotes, or in an unquoted context and precede it with a backslash:要制作'文字,请将其放在双引号内,或放在未加引号的上下文中,并在其前面加上反斜杠:

find . -maxdepth 1 -mindepth 1 -type d -name "*'*" -print

...or the 100% identical but harder-to-read command line... ...或 100% 相同但难以阅读的命令行...

find . -maxdepth 1 -mindepth 1 -type d -name '*'\''*' -print

If you're just searching the current directory (and not its subdirectories), you don't even need find , just a wildcard ("glob") expression:如果您只是搜索当前目录(而不是其子目录),您甚至不需要find ,只需要一个通配符(“glob”)表达式:

ls *\'*

(Note that the ' must be escaped or double-quoted, but the asterisks must not be.) (请注意, '必须转义或双引号,但星号不能。)

If you want to do operations on these files, you can either use that wildcard expression directly:如果要对这些文件进行操作,可以直接使用该通配符表达式:

dosomethingwith *\'*
# or
for file in *\'*; do
    dosomethingwith "$file"
done

...or store the filenames in an array, then use that. ...或将文件名存储在一个数组中,然后使用它。 This involves getting the quoting just right, to avoid trouble with other weird characters in filenames (eg spaces):这涉及使引用恰到好处,以避免文件名中的其他奇怪字符(例如空格)出现问题:

filelist=( *\'* )
dosomethingwith "${filelist[@]}"
# or
for file in "${filelist[@]}"; do
    dosomethingwith "$file"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM