From: Unix Admin (Unix-admin@triad.rr.com)
Date: Fri Oct 04 2002 - 00:11:54 EDT
Thanks for all the replies. I am not sure which I will use just yet. I have tested many of them with success.
Here is the original question:
All,
I have a bunch of files in a directory named:
p_234394.1.discharg_all.H.SINGLE.0227300310
p_234424.1.discharg_all.H.SINGLE.0227300250
p_234431.1.discharg_all.H.SINGLE.0227300327
p_234432.1.discharg_all.H.SINGLE.0227300211
p_234452.1.discharg_all.H.SINGLE.0227300346
p_234453.1.discharg_all.H.SINGLE.0227300339
p_234454.1.discharg_all.H.SINGLE.0227300323
What I want to do is strip off the
p_234394.1.discharg_all.H.SINGLE. part
and add .ps to the end.
So it would look like this 0227300310.ps
How could I do this?
Here are the replys: (again thanks)
----------------------------------------------
joseph@tsatech.com <joseph@tsatech.com>
for file in `ls -1 /dirname | awk -F . '{print $6}'`
do
mv $file /dirname/${file}.ps
done
----------------------------------------------
Mike Allmen <mike.allmen@motorola.com>
#!/bin/sh
for i in *
do
mv $i `echo $i | cut -d. -f6`.ps
done
assuming all files are the exact same format as
the files listed here
----------------------------------------------
Aaron Bennett
for FILE in $(/bin/ls); do
NEWNAME=$(echo $FILE | cut -d'.' -f1).ps
mv $FILE $NEWNAME
done
----------------------------------------------
Vance Brasher
Something like the following. Echo FILENAME before doing the mv just incase the -f5 is wrong.
#!/bin/ksh
cd yourdirectory
for I in *
do
FILENAME=`echo $I | cut -d"." -f5`
mv $I $FILENAME.ps
done
----------------------------------------------
Richard Wright
# ls p_*
p_234394.1.discharg_all.H.SINGLE.0227300310
# for x in $(ls p_*)
> do
> mv $x $(echo $x | sed 's/.*\.\(.*\)/\1.ps/')
> done
# ls p_*
p_* not found
# ls *.ps
0227300310.ps
----------------------------------------------
Allan.Marillier@dana.com
#! /usr/bin/sh
ls *.discharg_all* | while read File
do
echo $File | awk -F . ' { print $6 } ' | while read NewName
do
echo "Renaming $File to $NewName.ps"
print mv $File $NewName.ps
done
done
----------------------------------------------
Stéphane Lachapelle, B.Sc.
for f in $(ls *SINGLE*)
do
f2=$(echo $f | awk -f\. '{print $nf}')
mv $f ${f2}.ps
done
----------------------------------------------
Eric E Shasteen <eric.shasteen@ped.gmeds.com>
Assuming there are no sub-directories and you want to process all files in the
directory:
Create a script as follows:
#!/usr/bin/ksh
for FILE in $(find YOU_DIR_PATH) ; do
NEWFILE=$(echo $FILE | awk -F. '{print $NF}')
mv $FILE ${NEWFILE}.ps
done
----------------------------------------------
Bill Thompson
Here's a Perl script I use all the time to rename files. If
you're familiar with Perl you can rename files any way you
want. This script was originally written by Larry Wall. I
found it in one of my Perl books.
For your purposes you would run the script with the
following syntax:
rename 's/^.*discharg_all.H.SINGLE.//; s/$/.ps/'
*discharg_all.H.SINGLE*
----------------------------------------------
Eef Hartman
I don't know any way to do this in "sh", though others may know, but in csh
you can use the :e modifier, like this (script or direct type-in):
foreach f ( p_*.1.discharg_all.H.SINGLE.* )
mv $f $f:e.ps
end
(indent only for clarity).
What this means is:
loop over all filenames, putting each one in variable "f"
rename $f (that filename) into ONLY the extension of $f, followed by .ps
I just tested it and this works.
Probably there also are solutions with awk or maybe even with find.
----------------------------------------------
Doug Wyatt
Just use something like this:
#!/usr/bin/csh
#
# # Syntax: cmd target_dir
#
cd $1 ;
set DIRLST=`ls p_*` ;
foreach Fname ($DIRLST)
Fname2=`echo $Fname | sed -e 's/.*\.\([0-9][0-9]*\)$/\1.ps/` ;
mv $Fname $Fname2 ;
end ;
----------------------------------------------
Thierry ITTY
for F in p_*SINGLE* ; do N=$(echo $F | sed -e 's/^.*SINGLE\.//' | sed -e
's/$/.ps/') ; mv $F $N ; done
the 1st sed removes anything before SINGLE. and the 2nd adds the .ps extension
you get the new name in N, then do a mv old_name (F) new_name (N)
if unsure try
for F in p_*SINGLE* ; do N=$(echo $F | sed -e 's/^.*SINGLE\.//' | sed -e
's/$/.ps/') ; echo mv $F $N ; done
before to see but not make the changes that will occur (this will help you
test your sed patterns)
----------------------------------------------
Taylor, Vince
#!/bin/sh
for a in *
do
b=`awk -F. '{print $5".ps"}'`
echo "Renaming file $a to $b
mv $a $b
done
----------------------------------------------
Epstein, Kevin
something like this: (Not tested. You will have to give a test before rolling it out live
#!/usr/bin/sh
cd /my/dir/the/files/are/in
for i in ls
do
NAME=$(echo $i |cut -f6 -d".")
mv $i $NAME.ps
done
----------------------------------------------
Brett E. Wright <wrighbe@hp7004.ecae.stortek.com>
#!/usr/bin/csh -f
foreach file ( p_* )
mv $file ${file:e}.ps
end
----------------------------------------------
Sohr, James
#!/bin/sh
files=$(ls -1)
for file in $files
do
newname=$(echo file | awk -F'.' '{print $6;}')
mv $file ${newname}.ps
done
----------------------------------------------
Mark Schupsky <mark.d.schupsky@lmco.com>
If you use the c-shell, there is a :e feature that will strip everything off of a file
name except for the extension.
% set file=p_234394.1.discharg_all.H.SINGLE.0227300310
% echo $file
p_234394.1.discharg_all.H.SINGLE.0227300310
% echo $file:e
0227300310
% echo $file:e.ps
0227300310.ps
you'll probably get quite a few other ways of doing it with sed, awk or short scripts.
----------------------------------------------
Ganeshan_Bala@emc.com
#!/usr/bin/sh
for I in p_23*
do
mv $I ${I##.*}.ps
done
There are number of ways doing this but I think what I have here is probably the most elegant. You can also replace the p_23 phrase in the for statement with the appropriate wildcard name. Hope this helps.
----------------------------------------------
Jim McDonald
Untested this may work
cd <to where they are>
ls -1 | awk ' { FS="."} { printf "mv %s %s\n", $0, $6 } > TMP_LIST_FILE
sh TMP_LIST_FILE
rm TMP_LIST_FILE
#done
----------------------------------------------
Bill Hassell
This should do it:
#!/usr/bin/sh
cd /placeWHEREyourFILESare
PATH=/usr/bin
for MYFILE in $($PATH/ls
do
NEWNAME="$(echo $MYFILE | cut -f6 -d.).ps"
mv $MYFILE $NEWNAME
done
----------------------------------------------
TOTSCH,DAVID
try this:
for I in p_*
do
mv $I ${I##*.}.ps
done
You may desire to adjust the file name globbing you use to match the file names.
----------------------------------------------
McIntosh Alan
This should work...
for i in *SINGLE*
do
export new=`echo $i | awk -F. '{print $NF}'`
mv $i $new.ps
done
----------------------------------------------
-- ---> Please post QUESTIONS and SUMMARIES only!! <--- To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl Name: hpux-admin@dutchworks.nl Owner: owner-hpux-admin@dutchworks.nl Archives: ftp.dutchworks.nl:/pub/digests/hpux-admin (FTP, browse only) http://www.dutchworks.nl/htbin/hpsysadmin (Web, browse & search)
This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:20 EDT