count up file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count up file
# 1  
Old 11-02-2008
count up file

Hi i have a problem i am going to serch for files and i have a bash script
but the problem i have is that the file names are like bser00001, bser00002 etc. and when i serch for files it start to serch for bser1, bser2, etc how can i do so it counts 00001, 00002 etc.

i wrote in a while loop and n is counting from 0 to 10000

bser$n


second problem is when it have find the file then make a folder with the file name. if i find a file name bser00001.txt then the folder name would be bser00001


regards
Waqar
# 2  
Old 11-02-2008
Quote:
Originally Posted by waqar
etc how can i do so it counts 00001, 00002 etc.
second problem is when it have find the file then make a folder with the file name. if i find a file name bser00001.txt then the folder name would be bser00001
Hi,
consider using the programs seq and basename.

for example, seq -w 00001 4 or seq -w 1 00004 will give You a sequnce of whole numbers that will be the width of the widest number.

and basename $filename .txt will give You the bser00001 part of the name.

On the other hand, if You know that all files start with bser followed by five digits, You already have the name after using seq, I guess.

/Lakris
# 3  
Old 11-02-2008
i dont understand

Quote:
Originally Posted by Lakris
Hi,
consider using the programs seq and basename.

for example, seq -w 00001 4 or seq -w 1 00004 will give You a sequnce of whole numbers that will be the width of the widest number.

and basename $filename .txt will give You the bser00001 part of the name.

On the other hand, if You know that all files start with bser followed by five digits, You already have the name after using seq, I guess.

/Lakris
Thanx for your answer but i am realy bad at scripting...
hmmm i dont realy understand i am a newbi can you give me a exampel code ? the name is always start with bser and i will count from 00001 to 10000
how do i implement it in my code it is like


#!/bin/sh
n=0

while [[ $n != 10000 ]] ; do

i have a code here....... bser$n ................and then i have a grep function

because of i dont have the code here so i cant attach it
# 4  
Old 11-02-2008
Hi again,
You could do something like

Code:
#!/bin/bash
for x in $(seq -w 1 10000); do
grep "something" bser$x.txt
mkdir bser$x
echo finished with bser$x.txt
done

Could that be something to start with?

/Lakris

PS You can use a while too if You like, may be better for really big numbers...

Code:
#!/bin/bash
x=1
while [ $x -le 10000 ]  ; do
grep "something" bser$x.txt
mkdir bser$x
echo finished with bser$x.txt
x=$(($x+1))
done


Last edited by Lakris; 11-02-2008 at 05:42 PM..
# 5  
Old 11-02-2008
Quote:
Originally Posted by Lakris
Hi again,
You could do something like

Code:
#!/bin/bash
for x in $(seq -w 1 10000); do
grep "something" bser$x.txt
mkdir bser$x
echo finished with bser$x.txt
done

Could that be something to start with?

/Lakris
i think that would work but

for x in $(seq -w 1 10000); do

you set the intreval from 1 to 10000 or is it mean that the highest nar is 10000 and you count up 1 at every time?

thanx a lot for you answers Smilie

regards
Waqar
# 6  
Old 11-02-2008
I edited my last entry, so there's an example with while as well, if You refresh the page.

The for loop actually creates a list of numbers from 00001 (because of the -w parameter) to 10000, and the for loop executes once for each item in the list, setting x to that value. Thats why it may be better with the while variant because sometimes the list can be very large and in itself resource consuming.

look at it as

for x in 00001 00002 00003 00004 00005 ... and so on ... 09999 10000 ; do
something ...

/Lakris
# 7  
Old 11-02-2008
Quote:
Originally Posted by Lakris
I edited my last entry, so there's an example with while as well, if You refresh the page.

The for loop actually creates a list of numbers from 00001 (because of the -w parameter) to 10000, and the for loop executes once for each item in the list, setting x to that value. Thats why it may be better with the while variant because sometimes the list can be very large and in itself resource consuming.

look at it as

for x in 00001 00002 00003 00004 00005 ... and so on ... 09999 10000 ; do
something ...

/Lakris
Thanx very much i will try it tomorrow Smilie i hope it will work it will work with files with the name bser and have a interval from o to 100000000000000 if i change 10000 in the for loop or i mean in while loop.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Speed : awk command to count the occurrences of fields from one file present in the other file

Hi, file1.txt AAA BBB CCC DDD file2.txt abc|AAA|AAAabcbcs|fnwufnq bca|nwruqf|AAA|fwfwwefwef fmimwe|BBB|fnqwufw|wufbqw wcdbi|CCC|wefnwin|wfwwf DDD|wabvfav|wqef|fwbwqfwfe i need the count of rows of file1.txt present in the file2.txt required output: AAA 2 (10 Replies)
Discussion started by: mdkm
10 Replies

2. Shell Programming and Scripting

FASTEN count line of dat file and compare with the CTRL file

Hi All, I thinking on how to accelerate the speed on calculate the dat file against the number of records CTRL file. There are about 300 to 400 folder directories that contains both DAT and CTL files. DAT contain all the flat files records CTL is the reference check file for the... (3 Replies)
Discussion started by: ckwan
3 Replies

3. UNIX for Dummies Questions & Answers

Taking word count from file and printing in file

hi, i am having a file which contains the below content, i need to take the word count of if and print the file name also inputfile.txt file_name1.txt,type_name1.txt file_name2.txt,type_name2.txt i would need the word count of the files like this if file_name*.txt then wc -l... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

4. UNIX for Dummies Questions & Answers

To delete the oldest files in a file when file count in the folder exceeds 7

Hi All, I need to delete the oldest file in folder when the file count in the folder exceed 6 ( i have a process that puts the source files into this folder ) E.x : Folder : /data/opt/backup 01/01/2012 a.txt 01/02/2012 b.txt ... (1 Reply)
Discussion started by: akshay01987
1 Replies

5. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

6. Shell Programming and Scripting

Parse file from remote server to calculate count of string existence in that file

Hi I need to parse the file of same name which exist on different servers and calculate the count of string existed in both files. Say a file abc.log exist on 2 servers. I want to search for string "test" on both files and calculate the total count of search string's existence. For... (6 Replies)
Discussion started by: poweroflinux
6 Replies

7. Shell Programming and Scripting

appending the count of line in each file at head of each file

hello everybody, I have some files in directory.each file contain some data. my requirement is add the count of each line of file in head of each file. any advice !!!!!!!! (4 Replies)
Discussion started by: abhigrkist
4 Replies

8. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies

9. UNIX for Dummies Questions & Answers

how to get a file name & record count of csv file

HI , I am new to shell scripting , I have a requirement that I send a file for data quality ( original.csv) & i will be getting 4 files daily into a particular directory in return with cleansed data . the files may be clean.csv, unclean.csv , ... (2 Replies)
Discussion started by: sirik
2 Replies

10. UNIX for Dummies Questions & Answers

How to count the record count in an EBCDIC file.

How do I get the record count in an EBCDIC file on a Linux Box. :confused: (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question