0

I have two folders full of trainings and corresponding testfiles and I'd like to run the fitting pairs against each other using a shell script.

This is what I have so far:

for x in SpanishLS.train/*.train
do
    timbl -f $x -t SpanishLS.test/$x.test
done

This is supposed to take file1(-n).train in one directory, look for file1(-n).test in the other, and run them trough a tool called timbl. What it does instead is look for a file called SpanishLS.train/file1(-n).train.test which of course doesn't exist. What I tried to do, to no avail, is truncate $x in a way that lets the script find the correct file, but whenever I do this, $x is truncated way too early, resulting in the script not even finding the .train file.

How should I code this?

0

3 Answers 3

4

If I got you right, this will do the job:

for x in SpanishLS.train/*.train
do
    y=${x##*/}  # strip basepath
    y=${y%.*} # strip extention
    timbl -f $x -t SpanishLS.test/$y.test
done
Sign up to request clarification or add additional context in comments.

Comments

2

Use basename:

for x in SpanishLS.train/*.train
do
    timbl -f $x -t SpanishLS.test/$(basename "$x" .train).test
done

That removes the directory prefix and the .train suffix from $x, and builds up the name you want.

In bash (and other POSIX-compliant shells), you can do the basename operation with two shell parameter expansions without invoking an external program. (I don't think there's a way to combine the two expansions into one.)

for x in SpanishLS.train/*.train
do
    y=${x##*/}                                        # Remove path prefix
    timbl -f $x -t SpanishLS.test/${y%.train}.test    # Remove .train suffix
done

Beware: bash supports quite a number of (useful) expansions that are not defined by POSIX. For example, ${y//.train/.test} is a bash-only notation (or bash and compatible shells notation).

3 Comments

The substitutions are not Bash only, but POSIX sh, aren't they?
@tripleee Hmmm...yes, the ## and % (and # and %%) expansions are in POSIX shell. Another comment elsewhere suggests ${x//.train/.test}; that is not a POSIX expansion. I've updated my answer—thanks for pointing this out.
Thanks! That was way more elegant than I imagined!
0

Replace all occurences of .train in the filename to .text:

timbl -f $x -t $(echo $x | sed 's/\.train/.text/g')

2 Comments

can do this just in bash: -t ${x//.train/.test}
This does not deal with the SpanishLS.train vs SpanishLS.test path prefixes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.