外壳平等不输出任何结果

发布于 2025-01-30 11:59:55 字数 639 浏览 3 评论 0原文

我正在尝试通过文件目录进行迭代,并提取在5:10之间具有相同ID的文件,但并非相同的文件名。该脚本适用于第一个循环,但无法在第二个输出中识别任何合适的结果(退出而没有错误)。手动传递两个合适的文件名的示例无错误地运行。我不太确定这里怎么了。

$FILES="data/*"
for f in $FILES; do
    for g in $FILES; do
        if [[ ${f: 5: 10} == ${g: 5: 10} ]]; then
  
            if [[ ${g: -2} != ${f: -2} ]]; then

            echo "$f"
            echo "$g"                        
                    
            fi 
        fi
    done   
done


例如,如果包含数据/*:

data/worda_ln
数据/wordb_ln
数据/worda_ap
data/wordb_ap

脚本将输出:

data/worda_ln
数据/worda_ap


数据/wordb_ln
数据/wordb_ap

I'm trying to iterate over a file directory and extract files which have the same id in the range between 5: 10 but are not identical filenames. This script works for the first loop, but won't identify any suitable results in the second output (exits without error). Manually passing in an example of two suitable filenames runs without error. I'm not really sure what's wrong here.

$FILES="data/*"
for f in $FILES; do
    for g in $FILES; do
        if [[ ${f: 5: 10} == ${g: 5: 10} ]]; then
  
            if [[ ${g: -2} != ${f: -2} ]]; then

            echo "$f"
            echo "$g"                        
                    
            fi 
        fi
    done   
done


e.g. if the data/* contained:

data/wordA_ln
data/wordB_ln
data/wordA_ap
data/wordB_ap

The script would output:

data/wordA_ln
data/wordA_ap

data/wordB_ln
data/wordB_ap

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2025-02-06 11:59:55

这里的困难是您在同一文件上循环两次。
$ {var:5:10}将包括,worda_lnworda_cp。因此,这些永远不会平等。

无论如何,我采取了另一种方法。获取文件名列表,您要检查的部分:

find data -type f -exec basename {} \; | cut -d'_' -f1 | sort | uniq

ex:

  • 查找找到“ data/worda_ln”。
  • basename {}将返回“ worda_ln”。
  • |剪切-d'_'-f1将返回“ worda”。
  • |排序| uniq将删除重复。

从该可能的文件名列表中,在目录数据中进行另一个查找以列出匹配文件。

完整脚本:

#!/bin/bash

while IFS= read -r line
do
    find data -type f -name "*${line}*" -print
    echo ""
done < <(find data -type f -exec basename {} \; | cut -d'_' -f1 | sort | uniq)

The difficulty here is that you are looping twice on the same files.
And ${var: 5: 10} will include, for exemple, wordA_ln or wordA_cp. Therefore these will never be equal.

Anyway, I took another approach. Get a list of filenames, well the portion you want to check:

find data -type f -exec basename {} \; | cut -d'_' -f1 | sort | uniq

Ex:

  • find found "data/wordA_ln".
  • basename {} will return "wordA_ln".
  • | cut -d'_' -f1 will return "wordA".
  • | sort | uniq will remove duplicates.

From that list of possible filenames, do another find in directory data to list the matching files.

Complete script:

#!/bin/bash

while IFS= read -r line
do
    find data -type f -name "*${line}*" -print
    echo ""
done < <(find data -type f -exec basename {} \; | cut -d'_' -f1 | sort | uniq)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文