LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script (https://www.linuxquestions.org/questions/programming-9/shell-script-797130/)

himu3118 03-22-2010 04:40 PM

shell script
 
the shell script is like I have to increment r if file DAT00000r-* exists and also if DAT0000(r+1) exits then also i need to incement r. likr suppose r=71 I have three files DAT000071-000232,DAT000072-339,DAT000073-792791.then I want to make r=74. also there may be possibility that i file DAT000071-1798.job DAT000071-818.cut also exist with other files mentioned above. in this case also i want my r=74.

if i use :
for i in `(find . -name DAT*$r-*)`
do
r=$((r+1))
done

then it incements r by 5 as .job and .cut files are also dere. I want to increment it only once in presence of DAT000r-798 DAT000r-798.job DAT000r-798.cut.

what shud i do??
please post the soln as soon as possible. i need it quite urgently..
i'll be very thankful

penguiniator 03-22-2010 06:10 PM

You are incrementing r for each file found by find. You need to test somehow for the highest value of r in your filenames. Are they all sequentially numbered? If so, you may be able to sort them, extract the r value from the last filename, and increment it just once.

Or, you can create a sorted list of filenames and extract the starting value of r from the last filename, and then increment r one time.

grail 03-22-2010 07:10 PM

Code:

find -name "dat*" | awk -F- '{sub(/.*dat0*/,"",$1)}max < $1{max = $1}END{ print max }'

himu3118 03-23-2010 04:08 AM

@penguiniator:
no the files are not in sequence.If DAT909.6989.job DAT911.6989.job DAT909.6989.cut DAT913.6989 are there, then i need r to become 910 instead of 909 and next time it shud becom 912..

penguiniator 03-23-2010 04:29 AM

In that case, what you can do is use a sorted list of filenames. Starting with the lowest numbered pair of names, subtract the r values from each file name. If the difference is greater than 1, add one to the r value of the lesser of the two r values to get the value to use.

If the names are in a list sorted lowest to highest, compare files 0 and 1. Then 1 and 2, and so on until you find a pair of files who's r value difference is greater than one.

If you don't find any, add one to the r value of the last file name in the list.

grail 03-23-2010 05:13 AM

Are you able to give us the exact format of the file names or does it change as you have from post #1 to post #4?

himu3118 03-23-2010 05:56 AM

DAT000070-000000006 DAT000070-000000006.cut DAT000070-000000006.job files are exactly like this. here r=70.
DAT000075-000070709, DAT000073-000000006, DAT000070-000000006 DAT000070-000000006.cut DAT000070-000000006.job now i want to make r=71
then later
r=72
later r=74 as there is no file with r= 71 or 72 or 74

grail 03-23-2010 06:42 AM

Code:

find -iname "dat*" | awk 'BEGIN{FS="-";f=1;g=0}{sub(/.*DAT0*/,"",$1)}g && $1 != (num + 1){ num++;g=0}f{num = $1; f=0; g=1}END{print num}'


All times are GMT -5. The time now is 07:54 AM.