Error in looping through files

Hi,

I've got a folder with several files I'd like to manipulate. The file names are all ending in .txt and I'd like to loop through their names for manipulation. This is the script I've got so far:

for i in 'ls *.gtc.txt|cut -d "." -f1';
do
echo${i};
done

It should be easy enough, but the result looks like this:

ls
file1.gtc.txt
file2.gtc.txt
file3.gtc.txt
|
cut
-d
"."
-f1 

I've also tried this:

for i in 'ls *.txt|cut -d "." -f1';
do 
awk '{print $1,$2,$3,$4,$7$8,$9,$10}' ${i}.gtc.txt > ${i};
done

but the error I'm getting is this:

line 3: ${i}: ambiguous redirect

any ideas? what am I doing wrong?

Many thanks in advance!

You certainly want a `subshell` (backticks!) or $(subshell)

for i in `ls -d *.gtc.txt|cut -d "." -f1`
do
  echo "$i"
done 

A bit more elegant is to simply loop over the existing files and use (Posix-)shell-builtins

ext=gtc.txt
for i in *.$ext
do
  ishort=${i%.$ext}
  echo "$ishort"
done
1 Like

I see
it seems that I've just used the wrong symbol, so simple! as usual :slight_smile:
Many thanks!