如何从提到的列表中获取文件名

how to get the filename from the mentioned list

我是 linux and/or 脚本的新手,请多多包涵。我想要一个可以获取 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

这是一种方法。

>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 脚本以执行这些命令而不是打印它们。

这应该有效:

#!/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