LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-15-2013, 02:54 PM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Rep: Reputation: 1
rename folder


hi guys
i have a about 1000 directories like these and i want to rename all of them to be like output
input directories
Code:
p8( 18.30 - 19 )
p1( 12 - 14 )
p4( 18  - 21 )
p1( 16 - 18 )
p2( 14  - 18 )
p1( 20 - 22 )
p1( 10 - 12 )
p3( 17  - 19 )
p2( 7.30 - 8 )
p4 ( 6.30 - 7.30 )
.
.
.
.
output directories

Code:
p8( 18.20 - 18.50 )
p1( 11.50 - 13.50 )
p4( 17.50  - 20.50 )
p1( 15.50 - 17.50 )
p2( 13.50  - 17.50 )
p1( 19.50 - 21.50 )
p1( 9.50 - 11.50 )
p3( 16.50  - 18.50 )
p2( 7.20 - 7.50 )
p4 ( 6.20 - 7.20 )
.
.
.
.
e.g p3( 19 - 23 ) should rename to p3( 18.50 - 22.50 )
i wrote a simple code but i dont know how to expand that for this work

for example to change all 18 to 17.50


Code:

find . -type d | while read FILE
do
echo "$FILE" | grep 18 | sed 's/18/17.50/g'
done

and so on but my code does not make sense because 18 should be a counter from 1 to 23 and every time change in grep and sed to rename all directories .

or find numbers in directories' name with awk and wrote a case to check $2 and $4 and change all of them

find . -type d | while read FILE
do
echo "$FILE" |awk '/-/ { print $2 $4 }
done

any one have any idea to rename all that directories like output ???
any idea ????
 
Old 09-15-2013, 03:51 PM   #2
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, Ubuntu, Amazon Linux
Posts: 1,830
Blog Entries: 21

Rep: Reputation: 119Reputation: 119
I am not finding your explanation to be clear. Are the directory names the string of this formula like "p8( 18.30 - 19 )" with the spaces and parenthesis characters? And you want to change "19" to "18.50" in all directory names that have "19"? By rename do you mean the "mv" command? or are you just wanting a string substitution for output purposes (no change to the filesystem)?
 
Old 09-16-2013, 03:00 AM   #3
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
actually directories are like

P1 ( 9 - 10 ) = 111
P3 ( 17 - 19 ) = 113
P5 ( 2 - 4 ) = 115
P1 ( 22.30 - 23 ) = 111

numbers represent hour . i want to substitute all number with new value and mv all directories .

for example P3 ( 17 - 19 ) = 113 should rename to P3 ( 16.50 - 18.50 ) = 113
P3 ( 14 - 18 ) = 113 should rename to P3 ( 13.50 - 17.50 ) = 113

if directories were like p2( 14 - 18 ) i can detect 14 and 18 with sed d1 and d2 shows that

Code:
find . -type d -name "p*" | while  read i
do
        read d1 d2 < <(echo "$i" | sed 's/ //g;s/.*(\([0-9.]\+\)-\([0-9.]\+\))/\1 \2/;s/\./:/g')
but for directories like ( 13 - 17 ) = 113 how can i detect 13 17 with sed ?????
i want to use sed instead of awk !
 
Old 09-16-2013, 04:56 AM   #4
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
You already have a good answer here :

http://www.linuxquestions.org/questi...ce-4175477266/

try not to post same question multiple times.

Please mark the thread as SOLVED.
 
Old 09-16-2013, 06:05 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
I know you said you don't want awk, but it is a better fit than sed


this is really dumb....
but I think does what you want.

assumptions made,
  1. the numbers in ( ) represent time in hrs.min
  2. you want to 'reduce' by 10min
  3. when no mins there is no "."
  4. when there are min, they are never less than 10

Code:
find . -maxdepth 1 -type d -name "??*" | while read DIR;do awk '{
  n3=$3-.5;
  if ($3 ~ /\./)
      n3=$3-.1;
  n5=$5-.5;
  if ($5 ~ /\./)
      n5=$5-.1;
  {printf "mv \"%s\" \"%s %s %.2f %s %.2f %s %s %s\"\n",$0,$1,$2,n3,$4,n5,$6,$7,$8}}' <<< $DIR;done
in all honesty, that is awful.........
it is at best a cheap fix


really you should fix whatever creates the directories in the first place


show us the 'big picture' and we can fix it
 
1 members found this post helpful.
Old 09-16-2013, 06:41 AM   #6
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
thx guys for help

i wrote a script but i have a simple problem with that

i want to substitute some variables with new value
i used below code for substitution but didn't work

echo "P1 ( 13 - 15 ) = 110" | awk '( $3 = "$i1" ) { $5 = "$i2" } {print} '

in this thread
http://www.linuxquestions.org/questi...ce-4175477266/
shivaa wrote a script to solve my problem but that did not work too !!!
Code:
echo "P1 ( 13 - 15 ) = 110" | awk '{gsub($3,"$i1",$3) && gsub($5,"$i2",$5); print $0}'
output is "P1 ( $i1 - $i2 ) = 110" however i need this output

P1 ( 13.10 - 15.10 ) = 110

for example i1=13.10
and i2=15.10

any opinion ??

Last edited by mmhs; 09-16-2013 at 06:43 AM.
 
Old 09-16-2013, 07:08 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Where do 'i1' and 'i2' come from?

On a side note, whilst I know anyone create whatever they want, this has to be some of the more bizaar directory naming conventions. One could easily see issues occurring
at a later date
 
Old 09-16-2013, 07:42 AM   #8
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by grail View Post
Where do 'i1' and 'i2' come from?

On a side note, whilst I know anyone create whatever they want, this has to be some of the more bizaar directory naming conventions. One could easily see issues occurring
at a later date
i1 and i2 come form another code

i1=$(date --date="${d1} 10 minutes " "+%H.%M"
i2=$(date --date="${d2} 10 minutes " "+%H.%M"
 
Old 09-16-2013, 07:54 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by mmhs View Post
thx guys for help

i wrote a script but i have a simple problem with that

i want to substitute some variables with new value
i used below code for substitution but didn't work

echo "P1 ( 13 - 15 ) = 110" | awk '( $3 = "$i1" ) { $5 = "$i2" } {print} '

in this thread
http://www.linuxquestions.org/questi...ce-4175477266/
shivaa wrote a script to solve my problem but that did not work too !!!
Code:
echo "P1 ( 13 - 15 ) = 110" | awk '{gsub($3,"$i1",$3) && gsub($5,"$i2",$5); print $0}'
output is "P1 ( $i1 - $i2 ) = 110" however i need this output

P1 ( 13.10 - 15.10 ) = 110

for example i1=13.10
and i2=15.10

any opinion ??
what you want to do is seemingly random

I was under the impression you wanted to 'take off' 10mins ( the awk I gave does that )
But here you are adding 10mins

this will (almost) do what you want
Code:
i1=13.10
i2=15.10
echo "P1 ( 13 - 15 ) = 110" | awk '( $3 = '${i1}'  ) { $5 = '${i2}' } {print}'
giving this output
Code:
P1 ( 13.1 - 15.1 ) = 110
whereas
Code:
echo "P1 ( 13 - 15 ) = 110" | awk '( $3 = '${i1}'  ) { $5 = '${i2}' } {printf "%s %s %.2f %s %.2f %s %s %s\n",$1,$2,$3,$4,$5,$6,$7,$8}'
gives you
Code:
P1 ( 13.10 - 15.10 ) = 110
going back to your 'randomness'
what are your rules for reassigning 'the numbers'


In the long run it is probably easier if we see the whole of your script
Would also be nice to know why you need to do this in the first place.
is it a 'one off' or is this something you need to do routinely.
 
1 members found this post helpful.
Old 09-16-2013, 08:50 AM   #10
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
thx i solve the problem

Code:
j=$(echo "$i1" |awk '{gsub($3,"'$m1'",$3) && gsub($5,"'$m2'",$5); print }')

use '' instead of "" that works !!!!!!!!
 
Old 09-16-2013, 11:37 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Not to be too nit picky, but, if you are going to replace all of $[35] in the $[35] field with something, wouldn't it be easier to just say:
Code:
$3 = $m1
$5 = $m2
I will let you worry about the quoting.

Also, what is the point of the &&? There is no test nor action being performed based on this expression, so it would seem to serve no purpose
 
  


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
what actually happens when you rename a folder? rabbit2345 Linux - General 5 11-27-2009 01:30 AM
Folder Rename linn Linux - Newbie 26 10-29-2008 06:03 PM
How can I rename a folder's content to the name of the folder itself? xmrkite Linux - Software 7 07-25-2008 01:28 PM
Rename Folder in numerics infonlinebr Linux - Newbie 7 06-10-2008 03:02 PM
rename file/folder command hct224 Linux - Newbie 4 09-19-2003 10:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration