Solved

looking for shell to rename all files like ?????.dlm.???????.????.pip into ?????.dlm.pip.???????.????

Posted on 2003-12-04
4
425 Views
Last Modified: 2010-04-21
Thanks in advance,
-Al
0
Comment
Question by:AlexF777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9877914
for i in `ls ??????.dlm.???????.????.pip`
do
   mv $i ?????.dlm.pip.???????.????
done


i am assuming that instaed of ? u have some charecter there.
0
 
LVL 7

Expert Comment

by:glassd
ID: 9878488
I haven't tested this, so buyer beware.

$(ls ?????.dlm.???????.????.pip | awk '{
printf("mv %s %s%s%s\n",$0,substr($0,1,10),"pip",substr($0,10,13))
}')

the awk statement should print out three space separated fields for each line of input.
Field 1 is the string "mv" (the rename command).
Field 2 is the original filename (the whole line).
Field 3 is made up of the first 10 characters of the input file name (?????.dlm.), the string "pip", then the string starting with the 10th character of the filename, 13 characters long (.???????.????).

The input to the awk command is a list of all files of the required format (?????.dlm.???????.????.pip).

Finally, wrapping the whole lot in $(...) causes the shell (ksh or bash) to execute the output rather than just listing it to STDOUT.

Hope it works.
0
 
LVL 38

Accepted Solution

by:
yuzh earned 200 total points
ID: 9880474
This following little script should do the job for you:

#!/bin/ksh

for i in `ls ??????.dlm.???????.????.pip`
    N1=`echo $i | cut -d "." -f 1`
    N2=`echo $i | cut -d "." -f 3`
    N3=`echo $i | cut -d "." -f 4`
    mv ${i} ${N1}.dlm.pip.${N2}.${N3}
done

echo "Job done, thanks !"
exit

#   END OF SCRIPT



0
 
LVL 7

Assisted Solution

by:glassd
glassd earned 50 total points
ID: 9880568
Yes yuzh, using the period as the separator was so obvious that I missed it completely.

$(ls ?????.dlm.???????.????.pip | awk -F"."'{
printf("mv %s %s.%s.%s.%s.%s\n",$0,$1,$2,$5,$4,$3)
}')
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question