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 01-25-2013, 08:08 PM   #1
jroggow
Member
 
Registered: Mar 2006
Distribution: Slackware
Posts: 33

Rep: Reputation: 15
Probably String Parsing or Something


I'm trying to improve my bash scripting skills. Presently my scripts look like a brick shithouse. They work, but they ain't pretty.

My practice project is converting all of my music files to .ogg format and moving them into a dedicated directory.

I have a script to convert the files to .ogg and move them to my music directory, but I'm stumbling over a few details.

My script creates files named something like getbent.wav.ogg. Which is a bit ugly, so the move part of my script changes the file name to 'NUMBER'.ogg. NUMBER increments with each iteration so my music directory has 0.ogg, 1.ogg, 2.ogg, &c.

I'm fine with such a naming scheme since I don't need to care about the actual file name. I only need to care that my files aren't being overwritten. Given that my file's names are all numbers, I should be able to find the highest increment assigned to a file in the directory, assign my variable that value, plus one, and then continue the script as normal.

I can't for the life of me figure out how to find the highest increment in the directory and start the script anew from there. I could count the items in the directory listing, but since there is a significant possibility I will accidentally save something unrelated there, I would rather find the highest number in the file name of .ogg format.

How do I strip the extension and convert the remainder to an integer? And do I need to do that or am I over-thinking myself? Why are my socks wet? Have you seen my underpants? When will these questions end?

Anybody? Buhler?
 
Old 01-25-2013, 08:24 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
You want to find the highest number first:
Code:
lastfile=$(ls *.ogg | sort -g | tail -n 1)
Then strip the extension:
Code:
lastnum=${lastfile%.ogg}
You don't need to convert to integer, Bash is typeless.

You do need the sort -g. In ls 10.ogg comes before 3.ogg.

jlinkels

Last edited by jlinkels; 01-25-2013 at 08:26 PM.
 
Old 01-25-2013, 08:44 PM   #3
jroggow
Member
 
Registered: Mar 2006
Distribution: Slackware
Posts: 33

Original Poster
Rep: Reputation: 15
Curly braces are my new best friend. I was tinkering with regular expressions and SED.
 
Old 01-25-2013, 09:07 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Wink Anyone? Anyone?

Radioactive chickens will teach you how to spell Bueller.

See http://www.youtube.com/watch?v=uhiCFdWeQfA

Daniel B. Martin
 
Old 01-27-2013, 01:53 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Uggh, not ls again.

Code:
files=( *.ogg )
highest=${files[-1]}	#use ${files[@]:(-1)} in older bash versions

number="${highest%.*}"	#to simply remove the file extension

re='([0-9]+)'           #regex extraction of a number from a longer filename
[[ $highest =~ $re ]]
number=${BASH_REMATCH[1]}
However, this does require that the numbers are all zero-padded so that they automatically sort properly in the shell. Otherwise it gets more complex. I strongly recommend always using zero-padded numbers in filenames for this reason.

You can find a script I wrote that zero-pads file numbers in this thread:
http://www.linuxquestions.org/questi...rectory-949189

However, also be warned that the shell's arithmetic engine treats numbers with leading zeros as octal values, which will give you errors and bizarre-seeming outputs if you aren't aware of it. Check out this this page for more on how to handle that:

arithmetic expressions

See here too for more on using zero-padded numbers:
http://mywiki.wooledge.org/BashFAQ/018


And much more on bash's built-in string manipulation features here:
http://mywiki.wooledge.org/BashFAQ/100


Edit: another way to get the largest number, if not zero-padded:

Code:
greatest=0
for fname in *.ogg; do
   (( ${fname%.*} > ${greatest%.*} )) && greatest=$fname
done

echo "${greatest%.*}"


re='([0-9]+)' gtnum=0

for fname in *.ogg; do
    [[ $fname =~ $re ]] && fnum=${BASH_REMATCH[1]}
    (( fnum > gtnum )) && greatest=$fname && gtnum=$num
done

echo "$greatest ; $gtnum"

Last edited by David the H.; 01-27-2013 at 02:23 PM. Reason: stated
 
  


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
need help with string parsing using sed and awk amboxer21 Programming 5 04-03-2012 02:17 AM
[SOLVED] Parsing mountpoint from /etc/mtab (parsing "string") in C deadeyes Programming 3 09-06-2011 05:33 PM
Parsing String. msgforsunil Linux - Newbie 8 06-11-2009 01:47 PM
string parsing using perl bharatbsharma Programming 1 12-05-2007 06:04 AM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM

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

All times are GMT -5. The time now is 12:24 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