Stack Exchange
string manipulation not working in tcsh - don't understand variables of subprograms
first of all my code:
#!/bin/tcsh
#linebroadening, zero filling, Fouriertransform, Phase Correction, automatic baseline corr.,output
xyz2pipe -in fid/test%03d.fid -x -verb                \
| nmrPipe -fn EM -lb 20.0                             \
| nmrPipe  -fn ZF -auto                               \
| nmrPipe -fn FT -auto                                \
| nmrPipe -fn PS -p0 90.0 -p1 0.0 -di                 \
| nmrPipe  -fn POLY -auto                             \
| pipe2xyz -out ft2_01/test%03d.ft2 -ov -verb
\
#ROIs:
xyz2pipe -in ft2_01/test%03d.ft2 -x -verb             \
| nmrPipe  -fn EXT -x1 -109ppm -xn -120ppm -sw        \
| pipe2xyz -out ft2_roi/test%03d.ft2 -ov -verb
\
mkdir -p ft2_sum
mkdir -p ft2_sum_txt
proj2D.tcl -in ft2_roi/test001.ft2 -sum -out ft2_sum/test001
pipe2txt.tcl ft2_sum/test001/1H.dat > ft2_sum_txt/test001.txt

Short description of code until code line no. 17:
processing of raw 3D data which is saved into planes of 2D data as .ft2 files. Their file names are like that:
test001.ft2
test002.ft2
.
.
.
now I wanted to iterate with two different scripts over those files. Above is an working example how it works for a single file. Unfortunately I dont understand how this notation %03d works inside xyz2pipe. Is there a way to access this iteration method directly with the scripts proj2D.tcl and pipe2txt.tcl (maybe via shell pipes)?
So alternatively I tried to manipulate the filenames myself to iterate over all relevant files:
foreach file (ft2_roi/*)
  echo $file
  echo $file/ft2_roi
  echo ${file%.ft2}
  echo ${file#ft2_roi/}
end

which gives following output:
ft2_roi/test001.ft2
ft2_roi/test001.ft2/ft2_roi
Missing '}'.

So the echo ${file%.ft2} and echo ${file#ft2_roi/} string manipulation don't work correctly. What am I doing wrong?
Thanks for any advice or help!
best wishes,
Hannes

managed to do it with another program "sed":
foreach dir (ft2_roi/*)
  echo 'dir:' $dir
  set file = `echo $dir | sed 's/.*\///'`
  set filename = `echo $file | sed -E 's/(.*?)\..*/\1/'`
  echo 'filename:' $filename
end

output:
dir: ft2_roi/test001.ft2
filename: test001

question about an elegant way via %03d remains.

%03d looks like a printf format specifier. You can see this on your cmd line like printf '%03d\n" 2 should produce output like 002. The 0 means zero padded numbers, and 3 is the (max) length of the digit to be expected/processed cleanly and of course d means a digit, as appossed to s for string, f for float and some other values. Hope this helps. Good luck.
