Loop usage with counter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop usage with counter
# 1  
Old 02-01-2013
Loop usage with counter

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   # n 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


Last edited by Akshay Hegde; 02-01-2013 at 09:18 AM.. Reason: to change case as suggested by RudiC
# 2  
Old 02-01-2013
Sorry, I don't understand your requirement. And your code snippet not being related to your expected output doesn't really help (use of lower and upper case chars for variables interchangeably, introducing 2000 from the blue sky, ...).
Pls be more specific AND more careful in your writing/typing.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-01-2013
Please give us a full and short example by providing :
The name of the 2 directories
The name and the content of 4 corresponding files (2 in each directories)
And show us the output you expect.

I think it will then be easier for us to understand your requirements and help you
# 4  
Old 02-01-2013
I just wanted to try, something like this

if I have n number of folders, and each folder consisting n number of files, they say for instance I have work to append 1st line of each file in directory to single file xyz.txt, then without using cd command, how can I use those n files in each directory ?

appended file looks like this
Code:
folder_count  file count ....other column
1                       1
1                       2
1                       3
1                       4
2                       1
2                       2
2                       3
2                       4
2                       5
.
.
.
2                        n

here folder count and file count is just to know nth line (for further analysis containing some ip, date time )
is from nth folder of nth file

Last edited by vbe; 02-01-2013 at 10:12 AM.. Reason: corrected code tag
# 5  
Old 02-01-2013
I still don't understand what you are trying to achieve

Code:
for i in directory*
do n=$(find $i -type f | wc -l)
echo "dir $i has $n files in it"
done

# 6  
Old 02-01-2013
Do you mean:

Code:
for file in */*.dat; do
  head -n1 "$file"
done > xyz.dat

# 7  
Old 02-02-2013
Or do you mean
Code:
$ awk   'FNR==1 {match (FILENAME, /\/.*$/)
                 path = substr(FILENAME, 1, RSTART)
                 fn   = substr(FILENAME, RSTART+1)
                 if (path != oldpath) {d++; f=0; oldpath = path}
                 printf "%4d\t%03d\t%s\t%s\t%s\n", d, ++f, path, fn, $0
                }
        ' d=2000 $(ls */*.dat)
2001    001    dir1/   adapter2    BEGIN {RS="/"; FS="[= \"]+"}
2001    002    dir1/   adapter3    BEGIN {FS="="; OFS="\t"}...
.
.
.
2001    038    dir1/   tranposehugefile    infile:
2002    001    dir2/   date-handling       set -vx
2002    002    dir2/   findmissingfiles    ls  p...

AND, please, abstain from editing original requests making others' comments look stupid!

Last edited by RudiC; 02-02-2013 at 08:51 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop with limit counter

#!/usr/bin/ksh c=0 while ]; do echo /tmp/unex NOT found, iter : $c; ((c = $c + 1)); sleep 2; done so, the above counter doesn't work, already tried both -lt & -gt, and changed || to && so what am I missing? Thanks in advance (5 Replies)
Discussion started by: unexistance
5 Replies

2. Shell Programming and Scripting

Loop counter resets by itself

Hi! Can anyone explain this? The counter CDR_count should go on forever, but it suddenly resets at every step of the FOR loop (I know this because $_file has exactly 378 records). However, the counter reset is OUTSIDE the FOR loop. What's going on?? #!/bin/bash if || ; then echo ... (8 Replies)
Discussion started by: Flavius
8 Replies

3. Shell Programming and Scripting

Avoid multiple emails being sent - Counter, Loop?

Hello, I have currently coded a bash script below in which it does the following: # Archives compressed file from another location. Basically it moves *.gz files to another location. The script also sends an email whenever a new compressed file is placed. This is the issue that i... (5 Replies)
Discussion started by: Wizard_1979
5 Replies

4. Shell Programming and Scripting

How to implement the counter in loop?

Hi, I am working on a script where I need to add one functionality i.e. to could the number of tar files at particular location...but the script is working in below way. 1) create sandbox 2) Drop old member function 3) addmember function 4) Apply checkpoint lable 5) Resync operation(This... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

5. Shell Programming and Scripting

File merging based on different counter loop

hello, File 1 main Group sub group MIT VAR_1D_DATA_TYPE 23-03-2012 MIT VAR_1D_DATA_TYPE 22-03-2012 MIT VAR_10D_DATA_TYPE 23-03-2012 MIT VAR_10D_DATA_TYPE 22-03-2012 MIT ... (0 Replies)
Discussion started by: manas_ranjan
0 Replies

6. Shell Programming and Scripting

loop with a counter on a constant in bash

Hello Everyone, I'm in need of assistance on creating a script with a counter on a certain string. Basically this script opens a log file and displays certain log data. There are two key words in the log. START and FINISH. In between the START and FINISH is a variable ACTNUMBER. It will... (1 Reply)
Discussion started by: rxc23816
1 Replies

7. Shell Programming and Scripting

S# in a for loop - concatenate $(loop counter)

Hi, hope I am posting in the right section. My problem is that I have 2 or more arguments passed and I want to check if the arguments passed exists or not. The first argument should not exist and the remaining others should exist. example: ./shells.sh argument1 argument2 argument3 ... (5 Replies)
Discussion started by: fight4love
5 Replies

8. Shell Programming and Scripting

adding counter to a variable while moving in a loop

The scenario is like this : I need to read records from a file one by one and increment counter1, if a certain field matches with a number say "40"..the script should increment the counter2 and also extract a corresponding field from the same line and adding them one by one and redirecting the the... (5 Replies)
Discussion started by: mady135
5 Replies

9. Shell Programming and Scripting

Using the counter of a for loop in command line parameter

Say I have (in psuedocode) For i=1 to 10 tar cvfb /... 5*i /junk(i) end What I mean is that I want each successive for loop to have the block size parameter be 5 times the current counter. This isn't my actual code, just a stupid example...So the question is how do I descrive that parameter... (2 Replies)
Discussion started by: jeriryan87
2 Replies

10. Shell Programming and Scripting

Loop counter variable display

Hello everyone, how can I send output to the screen from a running script or tcl, in such a way that if a loop is executing I will see the rolling counter on my screen as the records are processed in the loop. I do not want the screen to scroll, though. In other words can a var's value be painted... (2 Replies)
Discussion started by: lifespan
2 Replies
Login or Register to Ask a Question