Solved

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

Posted on 2003-12-04
4
422 Views
Last Modified: 2010-04-21
Thanks in advance,
-Al
0
Comment
Question by:AlexF777
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix, date minus 1, 2 or 3 days (last working day) 11 68
Ubuntu install gnome-session-fallback is failing 4 114
NTP Server in VMware 5 286
awk sed variable in file 3 73
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

860 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