LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rename folder (https://www.linuxquestions.org/questions/linux-newbie-8/rename-folder-4175477226/)

mmhs 09-15-2013 01:54 PM

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 ????

Skaperen 09-15-2013 02:51 PM

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)?

mmhs 09-16-2013 02:00 AM

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 !

SAbhi 09-16-2013 03:56 AM

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.

Firerat 09-16-2013 05:05 AM

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

mmhs 09-16-2013 05:41 AM

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 ??

grail 09-16-2013 06:08 AM

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

mmhs 09-16-2013 06:42 AM

Quote:

Originally Posted by grail (Post 5028423)
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"

Firerat 09-16-2013 06:54 AM

Quote:

Originally Posted by mmhs (Post 5028411)
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.

mmhs 09-16-2013 07:50 AM

thx i solve the problem

Code:


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


use '' instead of "" that works !!!!!!!!

grail 09-16-2013 10:37 AM

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


All times are GMT -5. The time now is 10:56 PM.