LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-05-2008, 04:39 PM   #1
gub
Member
 
Registered: Feb 2005
Distribution: Mandriva 2007
Posts: 58

Rep: Reputation: 15
:newbish: hacking a nautilus script (egrep, awk)


Might belong in Newbie forum, but question about scripting.
After much googling and utilizing google cached page, I found a batch renaming script for nautilus here

Code:
#!/bin/bash
#
# multi-file rename script for Nautilus
# (c) Andrey Yurovsky http://andrey.thedotcommune.com/
#
# To install, place this script into your .gnome2/nautilus-scripts/ directory,
# which is located in your home directory.  'rename' should appear in your
# 'scripts' menu when you restart Nautilus (or log out and then log in again).
#
# Select one or more files in Nautilus, right-click one of the selected files, 
# and choose 'rename' from the 'scripts' menu.  A dialog box will ask for a 
# new name.  Enter a new name, with a starting index in square brackets 
# (ex: [1]).  The selected files will be renamed to this new name, incrementing
# the index each time.  For example, if the new name is "photo[1].jpg", the 
# files will be renamed "photo1.jpg", "photo2.jpg", and so on.
# ask for a name pattern
PATTERN=`zenity --entry --title="Rename Files" --text=name:`
if [ "${#PATTERN}" = "0"  ]; then
    exit
fi
# check that the pattern looks valid
if [ "`echo $PATTERN | egrep [^\[][\[][0-9]+[\]][^\]]`" != $PATTERN ]; then
    zenity --error --text="The name should contain a number in square brackets, for example 'photo[1].jpg' is valid."
    exit
fi

# extract the starting index
BNUM="`echo $PATTERN | awk '{printf( "%d-%d", M=match( $1,/[\[]([0-9]+)[\]]/ )+1, RLENGTH+M-3 );}'`"
NUM=`echo $PATTERN | cut -b $BNUM`

# extract the left and right parts of the new name
let L=${BNUM%-*}-2
let R=${BNUM#*-}+2
LEFT=`echo $PATTERN | cut -b 1-$L`
RIGHT=`echo $PATTERN | cut -b $R-`

# rename each file
for FILE in "$@"; do
    mv "$FILE" "$LEFT$NUM$RIGHT"
    let NUM+=1
done
After I added quotes to the $FILE in:
Code:
for FILE in "$@"; do
    mv "$FILE" "$LEFT$NUM$RIGHT"
    let NUM+=1
in order to do multiple files with spaces according to http://g-scripts.sourceforge.net/faq.php .

I wanted to do the following:

A.) Add support for spaces in naming the files-

B.) Start numbering at 01 after filename unless otherwise specified-

C.) Keep existing file extension unless otherwise specified-

Having no knowledge of "egrep" or "awk", and little if any real programming experience, I was wondering if this was possible.
If it is, how would I do it? Don't know how to contact Andrey Yurovsky.
Does anyone have any advice for me besides, "Learn to Code.",?
 
Old 02-03-2008, 08:04 AM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
I'm not sure what a nautilus script is or any special requirements it
has. However, I can show you how to work with files given as parameters
to a script that works with spaces in the name, matches wildcards, and
doesn't break if a file doesn't exist.


NAME_PREFIX="$1"
DIGITS="$2"
shift 2
ls "$@" | while read -r OLDNAME
do
#insert code to update $EXT and $NUM
move "$OLDNAME" "${NAME_PREFIX}${NUM}.${EXT}"
done
 
  


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
awk script help pls cmontr Programming 5 10-24-2007 10:49 PM
what does this awk script do? sharathkv25 Programming 3 03-08-2007 03:10 PM
diffrence between grep, sed, awk and egrep Fond_of_Opensource Linux - Newbie 3 08-18-2006 08:15 AM
About awk script sachin_keluskar Linux - Software 2 06-24-2005 03:19 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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