LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-23-2005, 11:37 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 392

Rep: Reputation: 31
stuck again (noob bash math question)


been playing with sorting files. file names are in format :

texttext_0001.bmp
texttext_0002.bmp
texttext_0003.bmp
texttext_0004.bmp
etc. into the thousands

have gotten as far as being able to isolate the numerical part of the filenames from the
extension and text preceeding the underscore. problem comes when i try to perform
math on the remaining numerical part i get errors because bash looks at this as octal
information. i knew how to use basic once upon a time to grab just the right part of the
string but can't figure this out for bash. how do i get bash to look at numbers with
leading 0's not as octal so i can work with them? either that or how do i strip out the
leading 0's?

for what it's worth, i'm making a loop that divides the numbers by 5 and sending the
files which are evenly divisible this way to another directory.

tia, and the script follows,
BabaG

#!/bin/bash

cd /home/babag/Buddies/Part-1

for i in *.bmp ; do
j=`echo "$i" | awk -F"." '{print$1}'`
k=`echo "$j" | awk -F"_" '{print$2}'`

if [ $((k % 5)) == 0 ]; then
mv $i /home/babag/Buddies/holding/$i
fi

done
 
Old 04-24-2005, 01:32 AM   #2
ogmoid
Member
 
Registered: Jul 2004
Location: Coeur d'Alene Idaho
Distribution: Slackware
Posts: 41

Rep: Reputation: 15
This is another way to do what you are trying, but does not directly address your question.

Since you are taking the files with numbers exactly divisible by 5, I believe you could do this:

mv *0.bmp *5.bmp /home/babag/Buddies/holding/
 
Old 04-24-2005, 02:04 AM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
try:
Code:
k=10#`echo "$j" | awk -F"_" '{print$2}'`
 
Old 04-24-2005, 02:11 AM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Code:
 l=`echo "$j" | awk -F"_" '{printf("%d", $2)}'`
 
Old 04-24-2005, 04:34 PM   #5
babag
Member
 
Registered: Aug 2003
Posts: 392

Original Poster
Rep: Reputation: 31
thanks for the help everyone. wound up using the printf version from osvaldo.
the 10# solution from CroMagnon shows the 10# characters when echoed to
the screen. both work and i've got both in the script so i can study. thanks
again. and ogmoid, i apreciate the tip but as i'm learning i wanted to use the
others as i thought i'd learn more from them in the long run. thanks again to
all.

BabaG
 
Old 04-25-2005, 12:42 AM   #6
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Yes, the 10# will echo to the screen if you print the variable, it only affects bash's treatment of the number for calculations. Since the only place you use $k is in the mod 5 calculation, I didn't think that would be an issue. Anyway, it's useful to know how to force bash to use a specific base - you can use up to base 64 if necessary.
 
Old 04-25-2005, 01:27 AM   #7
babag
Member
 
Registered: Aug 2003
Posts: 392

Original Poster
Rep: Reputation: 31
thanks cromagnon. all good info for this noob.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
noob bash script question babag Programming 9 04-30-2005 02:25 AM
bash and math division problem bennethos Programming 5 10-17-2004 01:51 PM
math/probubility/gambling question true_atlantis Programming 12 10-16-2004 04:21 PM
Noob, 3.0r2 First timer, stuck after login and PW Xentrix Debian 2 08-01-2004 04:31 AM
'security level' stuck [noob question] nocelery Linux - Security 2 04-08-2004 06:28 PM

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

All times are GMT -5. The time now is 06:58 PM.

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