Loop through files in a directory

Hello

How do I loop through files in a specific directory ?

This script is not working!

#! /bin/bash
FILES=/usr/desktop/input/*
For f in $FILES;
do
awk '-v A="$a" -v B="$b" {$6=($1-64)/2 ;$7=((10^($6/10))/A)^(1/B) ; print}' OFS="\t" $f > /root/Desktop/output/$f.txt;
done

Orginal txt file:

$1      $2            $3             $4
74   3.99166 101.37082  2.000
74   3.99166 101.37834  2.000
75   3.98416 101.37082  2.000
75   3.98416 101.37834  2.000
81   3.96916 101.29571  2.000
  0   3.96166 101.28820  2.000
 82   3.96166 101.29571  2.000
 85   3.96167 101.31073  2.000

Expected result:

$1         $2             $3                $4                  $5        $6
74	3.99166	101.37082	2.000		5	0.0748783
74	3.99166	101.37834	2.000		5	0.0748783
75	3.98416	101.37082	2.000		5.5	0.0804649
75	3.98416	101.37834	2.000		5.5	0.0804649
81	3.96916	101.29571	2.000		8.5	0.12391
0	3.96166	101.28820	2.000		-32	0.000364633
82	3.96166	101.29571	2.000		9	0.133155
85	3.96167	101.31073	2.000		10.5	0.165237

Thanks in advance

There are no upper case letters in bash keywords.

Second, your awk command line options are improperly quoted so that they and the awk script form a single token behind a leading dash.

Did running this code not generate errors? If so, always share those. They save everyone time and effort.

Regards,
Alister

Hi
First of all thanks for your response.

-I changed uppercase letter to lowercase, but not working again!
-I already run awk script lonely, its ok.
-The cod is not working, because it can not find input directory.

make sure that files exists using this

$ for f in $FILES; do echo $f; done

and where did you define variable a and b ?

1 Like

In addition, note that:

FILES=/usr/desktop/input/*
For f in $FILES;

is not the same as:

for f in /usr/desktop/input/*

Obviously For should be for (lowercase) like alister notes, but also, the content of the unquoted variable expansion $FILES is subject to field splitting (default: space, TAB or newline), which may lead to erroneous results and which will not occur with the second form...

1 Like

Hi friend
Thanks for your consideration.

when I run the code without copy the results, it is ok.

#! /bin/bash
FILES=/root/Desktop/decoder/input/*
for f in $FILES
do
awk '{$6=($1-64)/2 ;$7=((10^($6/10))/250)^(1/1.2) ; print}' OFS="\t" "$f";
done 

But when I wanna save the results in separate txt files, it is not working.

#! /bin/bash
FILES=/root/Desktop/decoder/input/*
for f in $FILES
do
awk '{$6=($1-64)/2 ;$7=((10^($6/10))/250)^(1/1.2) ; print}' OFS="\t" "$f" > "/root/Desktop/decoder/input/out/"$f".txt "
done 

I got this error:

[root@localhost decoder]# t1
./t1: line 6: /root/Desktop/decoder/input/out//root/Desktop/decoder/input/out.txt: No such file or directory
./t1: line 6: /root/Desktop/decoder/input/out//root/Desktop/decoder/input/SG1140103020247.CAP98RE.txt: No such file or directory
[root@localhost decoder]# 

Furthermore, I tried to put output directory after done , but it is also not working.

I have a lot of files in Input and I want to have the results of all the files in separate folder.

Thanks in advance.

If you are going to specify the path to the target directory in your redirection, then you need to strip directories from $f and only use the basename.

Regards,
Alister

Or you could make the destination directory before trying the redirect:

#! /bin/bash
FILES=/root/Desktop/decoder/input/*
for f in $FILES
do
  mkdir -p /root/Desktop/decoder/input/out$(dirname "$f")
  awk '{$6=($1-64)/2 ;$7=((10^($6/10))/250)^(1/1.2) ; print}' OFS="\t" "$f" > /root/Desktop/decoder/input/out"${f}.txt"
done