Bash种子,如何去除一组文件中每个文件名的第一个单词和扩展名



我正在测试一个应用程序,用于在呼叫中心对对话进行编目和存储。特别是,我正在准备将文件上传到数据库的材料。我们有数百万个文件。示例:

  • 音频_25-09-2018 00-08-33(1(.mp3
  • 信息_25-09-2018 00-08-33(1(.txt
  • 音频_25-09-2019 00-20-39(2(.mp3
  • 信息_25-09-2019 00-20-39(2(.txt
  • 音频_25-09-2020 00-20-39(1546879(.mp3

文件分为两对,记录和转录对话。我只需要复制那些有几个的。示例:

  • 音频_25-09-2018 00-08-33(1(.mp3
  • 信息_25-09-2018 00-08-33(1(.txt
  • 音频_25-09-2019 00-20-39(2(.mp3
  • 信息_25-09-2019 00-20-39(2(.txt

要做到这一点,我想获得两个列表并将它们相互比较:

  1. 转录文件的名称
  2. 录音的文件名

ls-1*.mp3 | sed-e的s/。[^_\d|\W]+\d?//'>list_mp3

我想接收文本:

  • _25-09-2018 00-08-33(1(
  • _25-09-2018 00-20-39(2(
  • _25-09-2018 00-20-39(3(

但我得到了:

  • 音频_25-09-2018 00-08-33(1(.mp3
  • 音频_25-09-2018 00-20-39(2(.mp3
  • 音频_25-09-2018 00-20-39(3(.mp3

我检查了表达式,它会准确地过滤我需要的内容。regex101的链接为什么它不能与sed一起工作?

最安全的方法是在纯bash中编写为:

for file in *; do
datetime=$file; datetime="${datetime#*_}"; datetime="${datetime%.*}"
# at this time you can do operations using $datetime and $file
done

所以你现在可以做这样的事情:

for file in *; do
datetime=$file; datetime="${datetime#*_}"; datetime="${datetime%.*}"
# at this time you can do operations using $datetime and $file
[ -f "Audio_${datetime}.mp3" ] && [ -f "Info_"${datetime}.txt ] && echo "we have both for ${datetime}"
done

最新更新