bash 用于导航目录子结构然后对 .xml 文件进行操作的脚本

bash script to navigate directory substructure and then operate on .xml files

我厌倦了这个:

for dir in /home/matthias/Workbench/SUTD/nytimes_corpus/NYTimesCorpus/2007/02/*/
    for f in *.xml ; do
        echo $f | grep -q '_output\.xml$' && continue # skip output files
        g="$(basename $f .xml)_output.xml"
        java -mx600m -cp /home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/stanford-ner-3.5.1.jar edu.stanford.nlp.ie.crf.CRFClassifier -loadClassifier /home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/classifiers/english.all.3class.distsim.crf.ser.gz -textFile $f -outputFormat inlineXML > $g
    done
done

这是基于对 this question 的回答,但这没有用。

我有一个文件夹结构,在目录 NYTimesCorpus 中有一个目录 2007,在该目录中有一个目录 01 以及 0203,等等...

然后在 01 中又出现了 01,02,03,...

在这些终端目录中的每一个中,都有许多我要应用脚本的 .xml 文件:

for f in *.xml ; do
    echo $f | grep -q '_output\.xml$' && continue # skip output files
    g="$(basename $f .xml)_output.xml"
    java -mx600m -cp /home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/stanford-ner-3.5.1.jar edu.stanford.nlp.ie.crf.CRFClassifier -loadClassifier /home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/classifiers/english.all.3class.distsim.crf.ser.gz -textFile $f -outputFormat inlineXML > $g
done

但是有太多不同的目录,运行将它放在每个目录中是一种罕见的折磨。除了 2007,我还有 20062005,所以理想情况下,我想做的是 运行 它一次,让程序自行导航该结构。

到目前为止我的尝试还没有成功,也许你们中有人知道如何实现这一目标?

感谢您的考虑。

更新

textFile=./scrypt.sh
outputFormat=inlineXML
Loading classifier from /home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/classifiers/english.all.3class.distsim.crf.ser.gz ... done [2.2 sec].
CRFClassifier tagged 71 words in 5 documents at 959.46 words per second.
CRFClassifier invoked on Sun Apr 12 19:33:34 HKT 2015 with arguments:
   -loadClassifier /home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/classifiers/english.all.3class.distsim.crf.ser.gz -textFile ./scrypt.sh -outputFormat inlineXML
    loadClassifier=/home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/classifiers/english.all.3class.distsim.crf.ser.gz

我会使用 find 因为它是递归工作的:

find /path/to/xmls -type f ! -name '*_output.xml' -name '*.xml' -exec ./script.sh {} \;

为了更好的可读性,我会把应该在每个文件上执行的操作保存到 script.sh:

#!/bin/bash

f=""
g="${f%%.*}_output.xml"
java -mx600m -cp /home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/stanford-ner-3.5.1.jar edu.stanford.nlp.ie.crf.CRFClassifier -loadClassifier /home/matthias/Workbench/SUTD/nytimes_corpus/NER/stanford-ner-2015-01-30/classifiers/english.all.3class.distsim.crf.ser.gz -textFile "$f" -outputFormat inlineXML > "$g"

并使其可执行:

chmod +x script.sh

find 是一个很好的解决方案。听起来所有 xml 文件都在同一目录深度,所以试试这个:

dir=/home/matthias/Workbench/SUTD/nytimes_corpus
for f in $dir/NYTimesCorpus/*/*/*/*.xml; do
    [[ $f == *_output.xml ]] && continue # skip output files
    g="${f%.xml}_output.xml"
    java -mx600m \
         -cp $dir/NER/stanford-ner-2015-01-30/stanford-ner-3.5.1.jar \
         edu.stanford.nlp.ie.crf.CRFClassifier \
         -loadClassifier $dir/NER/stanford-ner-2015-01-30/classifiers/english.all.3class.distsim.crf.ser.gz \
         -textFile "$f" \
         -outputFormat inlineXML > "$g"
done

glob 模式 $dir/NYTimesCorpus/*/*/*/*.xml 指定所需的 xml 文件正好比 NYTimesCorpus 低 3 级。那是错误的深度,然后更改模式中 */ 的数量。

如果 xml 文件可以出现在不同的深度,请使用 find,或者在 bash 中使用:

shopt -s globstar nullglob
for f in $dir/NYTimesCorpus/**/*.xml; do

reference