Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Find & Replace

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-11-2012
rob171171 rob171171 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 12 December 2012, 3:04 PM EST
Location: Dublin
Posts: 89
Thanks: 29
Thanked 0 Times in 0 Posts
Find & Replace

Hi
I am looking to rename the contents of this dir, each one with a new timestamp, interval of a second for each so it the existing format is on lhs and what I want is to rename each of these to what is on rhs..hopefully it nake sense


Code:
CDR.20060505.150006.gb    CDR.20121211.191500.gb
CDR.20060505.150109.gb    CDR.20121211.191501.gb
CDR.20060505.150216.gb    CDR.20121211.191502.gb
CDR.20060505.150308.gb    CDR.20121211.191503.gb

Hopefully this makes sense.
I know I need to utilise a sed command in conjunction with some sort of loop

Last edited by rob171171; 12-11-2012 at 02:20 PM.. Reason: more detail
Sponsored Links
    #2  
Old Unix and Linux 12-11-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 4 July 2015, 1:12 AM EDT
Location: Galactic Empire
Posts: 3,409
Thanks: 240
Thanked 1,217 Times in 1,143 Posts
How about?

Code:
for file in CDR*.gb
do
        echo "mv $file CDR.$( date +'%Y%m%d.%H%M%S' ).gb"
        sleep 1
done

Note: remove echo if the result is OK, replace like below:-

Code:
mv "$file" "CDR.$( date +'%Y%m%d.%H%M%S' ).gb"

The Following User Says Thank You to Yoda For This Useful Post:
rob171171 (12-11-2012)
Sponsored Links
    #3  
Old Unix and Linux 12-11-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 3 July 2015, 4:40 PM EDT
Location: Saskatchewan
Posts: 20,742
Thanks: 901
Thanked 3,691 Times in 3,450 Posts
sed is not the be-all and end-all of text. I'm not sure what I'd even use it for here since you're just replacing everything with everything...


Code:
read YYYYMMDD HH MM SS <<EOF
`date '+%Y%m%d %H %M %S'`
EOF

[ "${HH:0:1}" = "0" ] && HH="${HH:1}"
[ "${MM:0:1}" = "0" ] && MM="${MM:1}"
[ "${SS:0:1}" = "0" ] && SS="${SS:1}"

for FILE in *.gb
do
        echo mv "$FILE" "$YYYY-$HH$MM$SS.gb"

        let SS=SS+1

        [ "$SS" -gt 59 ] && SS=0 && let MM=MM+1
        [ "$MM" -gt 59 ] && MM=0 && let HH=HH+1
        [ "$HH" -gt 23 ] && HH=0
done

Remove the echo once you've tested and are sure it does what you want.

bipinajith's code is much simpler Linux But if you have 500 files, will take 8 minutes...
The Following User Says Thank You to Corona688 For This Useful Post:
rob171171 (12-11-2012)
    #4  
Old Unix and Linux 12-11-2012
rob171171 rob171171 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 12 December 2012, 3:04 PM EST
Location: Dublin
Posts: 89
Thanks: 29
Thanked 0 Times in 0 Posts
Thank you so much, that 1s 100% exactly what i was after. My approach was much different tyring to use sed and more complex solutions
Thanks again

---------- Post updated at 02:40 PM ---------- Previous update was at 02:39 PM ----------

Thank you Corona688 for your response,. You are correct in that Linux bipinajith' solution is far simpler.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find & Replace string in multiple files & folders using perl Zaheer.mic Shell Programming and Scripting 0 11-04-2009 12:08 PM
find & replace with user input mike909 Shell Programming and Scripting 3 10-06-2009 02:36 PM
Find & Replace string in multiple files & folders karthikn7974 Shell Programming and Scripting 4 04-29-2009 07:30 PM
find & incremental replace? treadwm Shell Programming and Scripting 2 04-21-2009 08:41 PM
Find & Replace gagansharma Shell Programming and Scripting 3 11-27-2001 03:17 PM



All times are GMT -4. The time now is 11:41 AM.