Loop usage with counter

Print Friendly

By Akshay Hegde

Hi friends, I just want to try some thing with shell using loop and counter

I have 30 Directory, in each directory number of files say 5, 10, 20 etc…

Code:

directory_1
directory_2
directory_n


what I want to do is read files from directory_1 say 5 files
if my counter is like this

Code:

m=2000 # M is for folder

n=1 # N is for file counter
for file in `ls *.dat |sort -t"_" -k2n,2`; do
awk -F, 'FNR == 1{print '$m',$1,$2,$3,'$n',$5,$6,$7}' OFS="," $file >>file.csv
n=$(( $n+1 ))
done


output wanted is something like this in single file comma separtated

Code:

column 1 column 2 $1 $2 $3 $..........$7
2000 1
2000 2
2000 3
2000 4
2000 5
2001 1
2001 2
2002 1


How can I use another counter in my script, also it has to read files from directory_1, directory_2 ….directory_n

Note : directory names are same only after

Code:

_


number is changing

so I think for this also one more counter is needed

Code:

something like this
x=1
cd directory_'x'

awk script

cd ..

increment x

cd directory_'x' # 2nd directory
awk script... so on


Source: FULL ARTICLE at The UNIX and Linux Forums

FavoriteLoadingAdd to favorites

Leave a Reply

Your email address will not be published. Required fields are marked *