LinuxQuestions.org
Help answer threads with 0 replies.
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-23-2008, 04:18 PM   #1
davimint
Member
 
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Rep: Reputation: 33
string comparison in bash


Hello everyone,
I wrote the following scrip to rename some jpg files to what I hope
is a better organized way to keeping up with them.

Code:
#!/bin/bash 
# Scrip to rename jpg files
num=1
x=$(date +%m%d%y)
skip=${x}
for file in *.jpg; do
    
    if [ "${file%_*.jpg}" = "$skip" ]; then
        echo Skipping file $file
        let num++
        continue 
    fi          

    if [ $num -le 9 ]; then
        mv "$file" "${x}_n00${num}.jpg"
        echo Moving $file
    fi 

    if  [ $num -ge 10 ]; then
        mv "$file" "${x}_n0${num}.jpg" 
        echo Moving $file
    fi 

    let num++

done
The way I have written the scrip is to use the date as the prefix
for the file and later on I plane to use "read" in the scrip so
I can enter other "older dates" for my archive of files.

Now, for my problem as you can see the first "if" statement skips the
files with the prefix of the current date. This is "not" what I want,
but for testing purposes it was what I could understand. I want the
first if statement to skip the files that have the "format" of the
date command. So any file that has "six numbers" before a "_" will be
skipped.

Thanks in advance.
 
Old 01-23-2008, 04:55 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by davimint View Post
I want the first if statement to skip the files that have the "format" of the date command. So any file that has "six numbers" before a "_" will be skipped.
I would extract the first six characters from the file name and then be sure they are all numbers using grep in this way:
Code:
if echo ${file:0:6} | grep -q ^[0-9]*$
then
    echo first six chars are numbers
else
    echo nope
fi
or something similar.

Just a side note: you can avoid the second and third if statements if you format the number by padding with leading zeros. One method using printf:
Code:
mv "$file" "${x}_n$(printf "%03u" $num).jpg"
Another method using an artifact:
Code:
num=1001
mv "$file" "${x}_n${num:1:3}.jpg"
 
Old 01-23-2008, 04:57 PM   #3
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
I'm not too good with bash, but you could try `expr`
Code:
expr "$file" : '[0-9]\{6\}_.*.jpg'
that will evaluate to something other then 0 if true.

If you have bash >=3.0 then you can just do
Code:
[[ "$file" =~ [0-9]{6}_ ]]

Last edited by angrybanana; 01-23-2008 at 04:58 PM.
 
Old 01-23-2008, 05:03 PM   #4
davimint
Member
 
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Original Poster
Rep: Reputation: 33
Thanks,
Those solutions look great.

David
 
Old 01-23-2008, 06:36 PM   #5
davimint
Member
 
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Original Poster
Rep: Reputation: 33
I just had to post the results of the new scrip.

Thanks angrybanana and colucix.

Code:
#!/bin/bash 
#
# Scrip to change jpg files for my cannon snapshot
#
num=1
echo "Enter you six digit month code"

read x

for file in *.jpg; do

   if echo ${file:0:6} | grep -q ^[0-9]*$; then
        echo skipping file  $file
        let num++
        continue
   else
        mv "$file" "${x}_n$(printf "%03u" $num).jpg"
        echo moving $file to "${x}_n$(printf "%03u" $num).jpg"
    fi
    let num++

done
Everything works great

Need to study up on the read command to make it bullet proof so
I don't enter something stupid.
 
  


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
String Comparison gjagadish Programming 6 11-06-2007 03:34 AM
String comparison in while [ ... ] condition hiwa Linux - Software 3 05-27-2007 02:05 AM
String comparison algorithm MicahCarrick Programming 4 04-11-2006 01:26 AM
bash string comparison noir911 Programming 1 01-25-2006 06:37 PM
Perl String comparison Xris718 Linux - General 5 04-01-2005 01:59 PM

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

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