Linux:从文件名获取特定字段

Linux:从文件名获取特定字段,linux,bash,Linux,Bash,我目前正在学习linux bash脚本: 我在文件夹中有文件,文件名模式如下: ABC01_-12ab_STRINGONE_logicMatches.txt DEF02_-12ab_STRINGTWO_logicMatches.txt JKL03_-12ab_STRINGTHREE_logicMatches.txt 我想将STRINGONE、STRINGTWO和STRINGTHREE提取为一个列表。要知道,如果我的想法行得通,我想首先向bash反映我的结果 我的bash脚本代码(在文件所在的文

我目前正在学习linux bash脚本:

我在文件夹中有文件,文件名模式如下:

ABC01_-12ab_STRINGONE_logicMatches.txt
DEF02_-12ab_STRINGTWO_logicMatches.txt
JKL03_-12ab_STRINGTHREE_logicMatches.txt
我想将STRINGONE、STRINGTWO和STRINGTHREE提取为一个列表。要知道,如果我的想法行得通,我想首先向bash反映我的结果

我的bash脚本代码(在文件所在的文件夹中执行):

实际结果:

error: unexpected end of file
预期结果:

STRINGONE
STRINGTWO
STRINGTHREE
(echoed in bash)

你现在的想法是对的。但是文件globbing(查找文本文件)和命令替换(运行
cut
命令)的语法是错误的。你需要做什么

for file in folder/*.txt; 
    # This condition handles the loop exit if no .txt files are found, and
    # not throw errors
    [ -f "$file" ] || continue
    # The command-substitution syntax $(..) runs the command and returns the
    # result out to the variable 'out'
    out=$(cut -d "_" -f3 <<< "$file")
    echo "$out"
done
用于文件夹/*.txt中的文件;
#如果未找到.txt文件,此条件将处理循环退出,并且
#不要抛出错误
[-f“$file”]| |继续
#命令替换语法$(..)运行该命令并返回
#结果输出到变量“输出”

out=$(cut-d“-f3您缺少一堆
for file in folder/*.txt; 
    # This condition handles the loop exit if no .txt files are found, and
    # not throw errors
    [ -f "$file" ] || continue
    # The command-substitution syntax $(..) runs the command and returns the
    # result out to the variable 'out'
    out=$(cut -d "_" -f3 <<< "$file")
    echo "$out"
done