两个Bash / Applescript需要读取和操作文件夹中的文本文件

时间:2011-10-11 03:11:23

标签: bash unix applescript

我有几十个文本(.txt)文件,其中包含我想要使用OS X Lion“Text to Speech”转换为音频文件的长词列表(每行一个)。为此,我需要添加合成语音标记标记来控制语音定时。

脚本1

以下是我在.txt文件中的格式:

Word1
Word2
Word3

以下是创建音频文件所需的内容:

Word1
[[slnc 600]]

[[slnc 900]]
Word1

[[slnc 3000]]

Word2
[[slnc 600]]

[[slnc 900]]
Word2

[[slnc 3000]]

Word3
[[slnc 600]]

[[slnc 900]]
Word3

[[slnc 3000]]

...etc,

文本文件位于我的桌面上名为“Words”的文件夹中。如果可能的话,如果脚本可以指向此文件夹并被告知迭代其中的每个.txt文件,执行上述更改,那就太棒了。

脚本2

这个需要从名为“French”的Desktop文件夹中的.txt文件中以制表符分隔的单词/短语进行读取。这是制表符分隔的.txt文件格式:

FrenchWord/Phrase1   EnglishWord/Phrase1
FrenchWord/Phrase2   EnglishWord/Phrase2

...etc,

然后输出为:

say "FrenchWord/Phrase1" using "Thomas"
delay 3
say "EnglishWord/Phrase1" using "Daniel"

delay 5

say "FrenchWord/Phrase2" using "Thomas"
delay 3
say "EnglishWord/Phrase2" using "Daniel"

delay 5

...etc,

由于这种情况下的.txt输入文件包含单个单词和短语,我猜这个脚本需要抓取所有“左边的标签分隔符”作为法语,并且一切都是正确的 - tab-delimiter'as'English。'

非常感谢任何帮助!:)

干杯,

戴夫

1 个答案:

答案 0 :(得分:0)

$ cat words.txt 
Word1
Word2
Word3
$ ./script1 words.txt # will produce words-with-timings.txt
$ cat words-with-timings.txt 
Word1
[[slnc 600]]

[[slnc 900]]
Word1

[[slnc 3000]]

Word2
[[slnc 600]]

[[slnc 900]]
Word2

[[slnc 3000]]

Word3
[[slnc 600]]

[[slnc 900]]
Word3

[[slnc 3000]]

$ cat phrases.txt 
FrenchWord/Bon jour EnglishWord/Good day
FrenchWord/Bon mot  EnglishWord/Well met
$ ./script2 phrases.txt # will produce phrases-with-timings.txt
$ cat phrases-with-timings.txt 
say "FrenchWord/Bon jour" using Thomas
delay 3
say "EnglishWord/Good day" using Daniel

delay 5

say "FrenchWord/Bon mot" using Thomas
delay 3
say "EnglishWord/Well met" using Daniel

delay 5

SCRIPT1:

#!/bin/bash

for wordfile_txt in "$@"
do

  wordfile_with_timings_txt=`echo $wordfile_txt | sed s/.txt/-with-timings.txt/`

  # Refuse to overwrite
  if [[ "$wordfile_txt" == "$wordfile_with_timings_txt" ]]
  then
    echo ".txt files only pls"
    exit 1
  fi

  while read word
  do
    echo $word
    echo '[[slnc 600]]'
    echo
    echo '[[slnc 900]]'
    echo $word
    echo
    echo '[[slnc 3000]]'
    echo
  done < $wordfile_txt > $wordfile_with_timings_txt

done

SCRIPT2:

#!/bin/bash

for phrasefile_txt in "$@"
do

  phrasefile_with_timings_txt=`echo $phrasefile_txt | sed s/.txt/-with-timings.txt/`

  # Refuse to overwrite
  if [[ "$phrasefile_txt" == "$phrasefile_with_timings_txt" ]]
  then
    echo ".txt files only pls"
    exit 1
  fi

  while read line
  do
    phrase1="`echo "$line" | cut -f 1`"
    phrase2="`echo "$line" | cut -f 2`"

    echo say \"$phrase1\" using "Thomas"
    echo delay 3
    echo say \"$phrase2\" using "Daniel"
    echo
    echo delay 5
    echo
  done < $phrasefile_txt > $phrasefile_with_timings_txt

done

要批量运行这些,我建议您使用findxargs

$ find lotta-words -type f
lotta-words/words1.txt
lotta-words/words2.txt
lotta-words/words3.txt
$ find lotta-words -type f | xargs ./script1 
$ find lotta-words -type f
lotta-words/words1-with-timings.txt
lotta-words/words1.txt
lotta-words/words2-with-timings.txt
lotta-words/words2.txt
lotta-words/words3-with-timings.txt
lotta-words/words3.txt