lhausmann
lhausmann

Reputation: 5

Shell Script: Truncating String

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?

Upvotes: 0

Views: 1561

Answers (3)

Alan
Alan

Reputation: 512

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

Upvotes: 4

Jonathan Leffler
Jonathan Leffler

Reputation: 753625

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).

Upvotes: 2

Igor Chubin
Igor Chubin

Reputation: 64563

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

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

Upvotes: 0

Related Questions