The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
rename files help piltrafa UNIX for Dummies Questions & Answers 2 10-04-2007 09:47 AM
trying to rename the files in dir hankooknara Shell Programming and Scripting 8 07-02-2007 03:36 AM
rename many files fsmadi SUN Solaris 4 04-30-2007 11:27 AM
How to rename files? CompuTelSystem UNIX for Dummies Questions & Answers 9 05-14-2002 03:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-30-2008
ajp7701 ajp7701 is offline VIP Member  
Supporter
  
 

Join Date: Dec 2007
Posts: 63
rename a lot of files again

here I go again...kinda hard to explain so I apologize.
I need to rename a bunch of files in a directory. I need to remove the first three characters of the filename, and then toward the end of the filename there is constant text inside of brackets. here is a demo (not for real) 'ls -1' of the dir:


001 Random name here - random title here - [constanttxt] f g- f.txt
002 another random name here - more random txt here [constanttxt] dg .txt
003 this is 3rd random name - 3rd random-title[constanttxt] d- df .txt
.
.
.
100 and and and the hundred names - hundred titles [constanttxt] s a .txt


Ok well its something like that, pretty ugly filenames! Anyway for now I just need the begining three numbers removed from each filename, and the [constanttxt] removed. They are all .txt files. Notice near the end of each filename the spacing is kinda random also, sometimes theres even random hyphens in the titles. Hope it makes sense. this is Fedora8, bash shell, and I have played around with awk and sed, but awk and sed dont seem to play nice with the bracket [] characters? Not only that but the mv and/or cp commands dont seem to play nice with spaces in the filenames thus I've really lost hope of automating this! Any pointers would help! Thank you so much!!!

Last edited by ajp7701; 03-30-2008 at 08:28 PM..
  #2 (permalink)  
Old 03-30-2008
ajp7701 ajp7701 is offline VIP Member  
Supporter
  
 

Join Date: Dec 2007
Posts: 63
example from research

I found this script that seems to be doing something remotely similar what i want to do, it is renaming a bunch of files with numbers at the front...so I can probably tweak on it a bit and get close to what desired, but still my fight is with the [] and the spacing in the filenames.....
ps, whoever wrote this good job!!

#!/bin/sh
ls |
egrep "^[0-9][0-9][0-9][0-9][0-9]" |
while read a
do
yahoo=`echo $a |sed 's/^[0-9][0-9][0-9][0-9][0-9][0-9][ .]//gi' | sed 's/^[kms]b[cs][^.]*\.//gi'`
echo $yahoo
mv "$a" "$yahoo"
done
  #3 (permalink)  
Old 03-30-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
Hi,

The filenames are not pretty, so I'd suggest to keep those files in one directory or rename them appropriately. Assuming all and only of the above files are in one directory :

Code:
for i in *
do 
   mv "$i" "$(echo "$i" | awk '{gsub(/\[constanttxt\]/,""); $1=""}1')"
done
Tested on Fedora 8.

Last edited by rubin; 03-30-2008 at 09:30 PM.. Reason: added : only of the ...
  #4 (permalink)  
Old 03-30-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Here is my solution....
Code:
$ cat fix
#! /usr/local/bin/bash

shopt -s extglob

ls |while IFS="" read name ; do
        if [[ $name = *.txt ]] ; then
                newname=${name##+([0-9]) }
                newname=${newname/\[*\]/}
                mv "$name" "$newname"
        fi
done
$
$
$
$
$ ls -1
001 Random name here - random title here - [constanttxt] f g- f.txt
002 another random name here - more random txt here [constanttxt] dg .txt
003 this is 3rd random name - 3rd random-title[constanttxt] d- df .txt
c
fix
$
$
$
$
$ ./fix
$
$
$
$ ls -1
Random name here - random title here -  f g- f.txt
another random name here - more random txt here  dg .txt
c
fix
this is 3rd random name - 3rd random-title d- df .txt
$
  #5 (permalink)  
Old 03-31-2008
ajp7701 ajp7701 is offline VIP Member  
Supporter
  
 

Join Date: Dec 2007
Posts: 63
thx!

thanks! that looks great. Hey perderabo even though I dont recognize the shopt -s extglob command your output looks great! thx!! one of my problems was i didnt know how to deal with the [brackets] SO THANK YOU THANK YOU THANKYOU!!!!
  #6 (permalink)  
Old 03-31-2008
ajp7701 ajp7701 is offline VIP Member  
Supporter
  
 

Join Date: Dec 2007
Posts: 63
oops...

Perderabo's script:
Ok the script works great...but as it turns out for many of my files there is also a hyphen between the first three digit number and the name....so the mv command goes to move it to the $newname which then begins with a hyphen and gives an error 'unknown option --'
How do I deal with that one?? :-)
thx again for the help! The script was awesome with all the ##+([0-9 stuff! dang I never would have figured that out.
I need to find a good reference for all these special characters and what they mean.
  #7 (permalink)  
Old 03-31-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Code:
ls *.txt | awk '{
  f = $0
  gsub("(^[0-9][0-9][0-9]|\[.*\])", "", $0)
  system("mv \""f"\" \""$0"\"")
}'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0