Find & Replace | Unix Linux Forums | UNIX for Dummies Questions & Answers

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 12-11-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 27 July 2014, 11:40 PM EDT
Location: Galactic Empire
Posts: 3,356
Thanks: 230
Thanked 1,190 Times in 1,123 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 12-11-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 July 2014, 10:58 PM EDT
Location: Saskatchewan
Posts: 18,978
Thanks: 748
Thanked 3,146 Times in 2,951 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 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 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 bipinajith' solution is far simpler.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 08:21 AM.