【问题标题】:how to get the filename from the mentioned list如何从提到的列表中获取文件名
【发布时间】:2015-06-17 13:30:54
【问题描述】:

我是 linux 和/或脚本的新手,所以请耐心等待。我想要一个可以获取 Linux 目录文件的脚本。这是我尝试获取文件名的方法。

for NAME in $(ls -1 *.wav /some/path | cut -d "/" -f3 | cut -d "-" -f1-5)

如果文件名包含 -IN 或 -OUT 那么它们将是 sox -m 之后是 mv 到另一个目录,但如果是其他一些文件,那么它将只是 mv

供参考,文件名如下

1030-04-06-2015-1433414216.wav
1030-04-06-2015-1433414318.wav
1030-04-06-2015-1433414440.wav
1043-21-05-2015-1432207256.wav
1043-21-05-2015-1432207457.wav
1046-20-05-2015-1432137944.wav
1046-20-05-2015-1432138015.wav
1046-20-05-2015-1432138704.wav
1431709157.93900.0-in.wav
1431709157.93900.0-out.wav
1431709157.93900.1-in.wav
1431709157.93900.1-out.wav
1431710008.94059.0-in.wav
1431710008.94059.0-out.wav
1431710008.94059.1-in.wav
1431710008.94059.1-out.wav
1431710008.94059.1.wav
1431710008.94059.2.wav
1431713190.94698.2-in.wav
1431713190.94698.2-out.wav
1431713190.94698.2.wav
1431721107.96010.0-in.wav
1431721107.96010.0-out.wav
1431721107.96010.1.wav

【问题讨论】:

  • 到目前为止,您是否尝试过为此编写任何内容?如果有,请向我们展示您的尝试。
  • title 说:how to get the filename from the mentioned list,上面提到的列表在哪里?在一个文件中?或者你只想要目录中的每个文件?或者可能带有 .wav 扩展名?如果您要做的只是向其他人索取代码,您至少应该明确和明确您的要求......
  • @EtanReisner,用 FINDCUT 尝试了一些我想要的东西BASENAME 但正如您所见,我已经在所有文件中添加了“-”。
  • @Jahid 我有一个 目录,其中包含波形文件,文件名如我的问题所示。
  • 请至少向我们展示您的尝试。

标签: linux bash filenames


【解决方案1】:

这应该可行:

#!/bin/bash
regex='(.*)(-out|-in)(.txt)'
for file in *.txt;do
# $file is the full path to the file
  if [[ $file =~ $regex ]];then
  #filename in this block contains -in or -out 
    filename="${file##*\/}"
    inorout="${BASH_REMATCH[2]}"
    extension="${BASH_REMATCH[3]}"
    filenamewithoutextension="${filename%.*}"
    filenamewithoutioroutorextension="${filenamewithoutextension/%$inorout/}"
    filenamewithoutiorout="${filenamewithoutioroutorextension}$extension"
    echo "$filename" "$inorout" "$extension" "$filenamewithoutextension" "$filenamewithoutiorout" "$filenamewithoutioroutorextension"
    #do something here sox-m mv or whatever
  else
  #filename in this block doesn't contain -in or -out
    echo "do something else"
  fi
done


解释:

"${file##*\/}" 是从$file 切割*/ 留下的字符串,即基本名称(文件名)。

"${BASH_REMATCH[2]}"[[ $file =~ $regex ]] 完成的模式匹配中第二个捕获的组,即-in-out

"${BASH_REMATCH[3]}" 是第三个捕获的组,即.wav

"${filename%.*}" 是从$file 剪切.* 留下的字符串,即没有.wav 的文件名


您应该检查的资源:
  1. Bash Parameter Expansion
  2. Pattern Matching
  3. Bash Variables

【讨论】:

  • 谢谢@Jahid。但您可能知道,我想要的是不带 -IN/-OUT 的 BASIC 文件名,这样我就可以混合 IN/OUT 并命名输出 BASIC 文件名。
  • @AamirAhmed 我添加了另外两个变量:filenamewithoutioroutfilenamewithoutioroutorextension
【解决方案2】:

这是一种方法。

>cat test.sh
#!/bin/bash

destination="./DEST"

# Loop over every file
for file in "${@}" ; do
  # If it is "-in", then sox
  if [[ "${file}" =~ "-in" ]] || [[ "${file}" =~ "-out" ]] ; then
    printf "sox -m ${file}; "
  fi
  echo "mv ${file} ${destination}"
done

运行时,我得到以下输出。

>../test.sh *
mv 1030-04-06-2015-1433414216.wav ./DEST
mv 1030-04-06-2015-1433414318.wav ./DEST
mv 1030-04-06-2015-1433414440.wav ./DEST
mv 1043-21-05-2015-1432207256.wav ./DEST
mv 1043-21-05-2015-1432207457.wav ./DEST
mv 1046-20-05-2015-1432137944.wav ./DEST
mv 1046-20-05-2015-1432138015.wav ./DEST
mv 1046-20-05-2015-1432138704.wav ./DEST
sox -m 1431709157.93900.0-in.wav; mv 1431709157.93900.0-in.wav ./DEST
sox -m 1431709157.93900.0-out.wav; mv 1431709157.93900.0-out.wav ./DEST
sox -m 1431709157.93900.1-in.wav; mv 1431709157.93900.1-in.wav ./DEST
sox -m 1431709157.93900.1-out.wav; mv 1431709157.93900.1-out.wav ./DEST
sox -m 1431710008.94059.0-in.wav; mv 1431710008.94059.0-in.wav ./DEST
sox -m 1431710008.94059.0-out.wav; mv 1431710008.94059.0-out.wav ./DEST
sox -m 1431710008.94059.1-in.wav; mv 1431710008.94059.1-in.wav ./DEST
sox -m 1431710008.94059.1-out.wav; mv 1431710008.94059.1-out.wav ./DEST
mv 1431710008.94059.1.wav ./DEST
mv 1431710008.94059.2.wav ./DEST
sox -m 1431713190.94698.2-in.wav; mv 1431713190.94698.2-in.wav ./DEST
sox -m 1431713190.94698.2-out.wav; mv 1431713190.94698.2-out.wav ./DEST
mv 1431713190.94698.2.wav ./DEST
sox -m 1431721107.96010.0-in.wav; mv 1431721107.96010.0-in.wav ./DEST
sox -m 1431721107.96010.0-out.wav; mv 1431721107.96010.0-out.wav ./DEST
mv 1431721107.96010.1.wav ./DEST
mv DEST ./DEST

如果要执行命令,只需将这些行剪切并粘贴到 shell 中,或修改 bash 脚本以执行这些命令而不是打印它们。

【讨论】:

  • 用你的脚本,它得到 /some/path/filename.wav 因为我只想要 BASENAME 来自源目录。
猜你喜欢
  • 2013-11-06
  • 1970-01-01
  • 2020-04-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多