我是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
答案 0 :(得分:0)
这是一种方法。
>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脚本以执行这些命令而不是打印它们。
答案 1 :(得分:0)
这应该有效:
#!/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
的文件名留下的字符串