LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2010, 04:40 PM   #1
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Rep: Reputation: 15
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
 
Old 03-22-2010, 06:10 PM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
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.
 
Old 03-22-2010, 07:10 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
find -name "dat*" | awk -F- '{sub(/.*dat0*/,"",$1)}max < $1{max = $1}END{ print max }'
 
Old 03-23-2010, 04:08 AM   #4
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
@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..
 
Old 03-23-2010, 04:29 AM   #5
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
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.
 
Old 03-23-2010, 05:13 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 03-23-2010, 05:56 AM   #7
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
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
 
Old 03-23-2010, 06:42 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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}'
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration