- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一个 bash 脚本来自动执行一些任务。我必须做的一件事是在目录中的文件名中搜索模式,然后遍历结果。
当我运行这个脚本时:
data=$(ls $A_PATH_VAR/*.ext | grep -o '201601[0-9]\{2\}\|201602[0-9]\{2\}')
echo $data
我得到了预期的结果 - 在 $A_PATH_VAR/
的文件名中找到的所有匹配列表,扩展名为 .ext
。但是,当我将所述模式存储在变量中然后使用它时,如下所示:
startmo=201601
endmo=201602
mo=$((startmo+1))
grepstr="'$startmo[0-9]\{2\}"
while [ $mo -le $endmo ]
do
grepstr="$grepstr\|$mo[0-9]\{2\}"
mo=$((mo+1))
done
grepstr="$grepstr'"
echo $grepstr # correct
data=$(ls $A_PATH_VAR/*.ext | grep -o $grepstr)
echo $data
$grepstr
中的模式被正确回显 - 也就是说,它包含值 '201601[0-9]\{2\}\|201602[0-9]\{2\}'
,但是 $data
是空的。这是为什么?
我的解决方案:
mo=$((startmo+1))
grepstr="($startmo[0-9][0-9]"
while [ $mo -le $endmo ]
do
grepstr="$grepstr|$mo[0-9][0-9]"
mo=$((mo+1))
done
grepstr="$grepstr)"
files=$(ls $A_PATH_VAR/*.ext)
setopt shwordsplit
for file in $files
do
if [[ $file =~ $grepstr ]]
then
date=$BASH_REMATCH
fi
...
done
最佳答案
在下面,我忽略了你的输入源是 ls
,除了这个开场白 ls
should not be used in this manner , 和 find
(在 GNU 扩展形式中,它包含一个 -regex
运算符)应该被考虑代替。
在:
pattern="'pattern'"
grep $pattern
...双引号 ("
) 是句法 - 它们在 shell 的解析阶段使用,而单引号在它们内部, 是文字 -- 外部的语法引号指定其中的所有内容都被视为字符串的一部分(除非解析双引号内容的规则不同)。
因此,当您运行 grep $pattern
时,会发生以下情况:
$pattern
的内容在 IFS 中被分解为任何字符的单词。默认情况下,IFS 只包含空格;但是,如果您有 IFS=a
,那么这将被分解为单词 "pa
和单词 ttern"
pattern
包含 "hello * world"
,并且您的默认值是 IFS 解析空白,我们将分解为单词 "hello
、*
和 world"
-- 然后 *
将替换为当前目录中的文件列表.显然,您不希望这样。因此,如果您的目标是防止字符串拆分和 glob 扩展,请仅使用句法引号:
pattern="pattern"
grep "$pattern"
顺便说一句,如果我有这个任务,我可能会这样写[以避免需要为每个可能的日期范围手动构建一个正则表达式]:
startmo=201601
endmo=201705
currmo=$startmo
# this requires GNU date
# on MacOS, you can install this via macports and invoke it as gdate
next_month() {
date -d "+1 month ${1:0:4}-${1:4:2}-15" +%Y%m
}
while [[ $currmo <= $endmo ]]; do
currmo=$(next_month "$currmo")
files=( *"$currmo"* )
[[ -e $files ]] || { echo "No files found for month $currmo" >&2; continue; }
printf '%s\n' "${files[@]}"
done
关于linux - ls | grep 变量作为正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37868835/
我有一个文件 test.log。非常大的日志文件。它有不同级别的日志记录。例如,trace , debug , info , warning和 error . 显然trace级别消息只是高速发送垃圾邮
我正在经历一些事情,发现了我无法理解的事情, grep -v grep 这意味着什么?我知道 -v 开关将选择所有不匹配的行。但为什么是第二个grep? 这是完整的命令: ps -ef | grep
我使用 egrep 输出一些带有平台名称的行: XXX | egrep "i686-nptl-linux-gnu$|i686-w64-mingw32$|x86_64-unknown-linux-gnu
grep退出状态部分报告中的手册: EXIT STATUS The exit status is 0 if selected lines are found, and 1 if not
我试图返回多个字符串的第一次出现,即,我想从以下文本中选择第一次出现 1259、3009 和 1589 的行。 ADWN 1259 11:00 B23 ADWN 3009
我猜它不是 Perl 兼容的正则表达式,因为有一种特殊的 grep具体来说就是 PCRE。什么是grep最相似? grep有什么特别的怪癖吗?我需要知道什么? (我习惯了 Perl 和 PHP 中的
有没有办法让 grep 从与搜索表达式匹配的文件中输出“单词”? 如果我想在多个文件中查找“th”的所有实例,我可以这样做: grep "th" * 但是输出会是这样的(粗体是我写的); some-t
我有许多(近 100 个)大 csv 文件,第一列中有 sellID。我知道某些 sellID 在 2 个或更多文件中重复 2 次或多次。是否可以使用 grep 找到所有重复的 sellID(创建映射
我有一个文件中的文件列表。该列表很大,并且文件名是非标准的:这意味着有些文件包含空格、非 ASCII 字符、引号、单引号... 因此,将庞大的文件列表作为参数传递给 grep 并不是一种选择: 因为我
我想在 shell 脚本中使用 grep 和两个变量 var = match cat list.txt | while read word_from_list; do grep "$word_
我有一个大文件,其中每一行都包含一个子字符串,例如 ABC123。如果我执行 grep ABC file.txt 或 grep ABC1 file.txt 我按预期返回这些行,但如果我执行 grep
当我执行以下 grep 时,我得到的结果我无法向自己解释: host:/usr/local/tomcat > grep '-XX:PermSize=256m' * RELEASE-NOTES:
这个问题在这里已经有了答案: grep recursively for a specific file type on Linux (5 个回答) 关闭4年前。 要在子目录中查找所有带有 .out 扩
有什么方法可以让我在搜索某些东西时使用 grep 忽略某些文件,相当于 svnignore 或 gitignore 的东西?我通常在搜索源代码时使用类似的东西。 grep -r something *
有没有办法让 grep 从匹配搜索表达式的文件中输出“单词”? 如果我想在多个文件中找到“th”的所有实例,我可以这样做: grep "th" * 但是输出会是这样的(粗体是我的); some-tex
我是 awk/sed 的完全菜鸟,所以如果我在这里遗漏了一些明显的东西,请原谅我。 基本上我正在尝试做一个嵌套的 grep,即类似于: grep $value `exim -Mvh $(`exim -
我正在尝试编写下载 node.js source 的脚本和 corresponding SHASUMS256.txt ,校验和,grep OK,不返回任何结果,使用 grep 的 -q 标志成功退出代
在 grep "str"* 这是否意味着 grep 执行 grep 的所有内容? 那么 grep -r "str". 的结果比前一个多 最佳答案 当您运行 grep str * 时,shell 将扩展
我正在尝试 grep 文件以查找匹配项的确切出现,但我也得到了更长的虚假匹配项: grep CAT1717O99 myfile.txt -F -w 输出: CAT1717O99 CAT1717O99
我有一个文件,其中包含我希望通过未指定分析运行的标识符(每行一个)。如果一切运行正常,分析将输出具有相同标识符的另一个列表(不一定按相同顺序)。 然而,事实证明,对某些输入标识符没有进行分析,并且这些
我是一名优秀的程序员,十分优秀!