Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing File Names using AWK

Status
Not open for further replies.

dayankoven

Programmer
Oct 28, 2002
17
MY
Hi there fellow gurus...

Am trying to write a korn shell script to do some filename manipulation. Say for example, in a working directory there will be 2 files with the following names ABC_200210111035.dat and ABC_200210121035.dat. What i need to do is to scan the directory, and replace all the ABC with 1001, so that end result would be 1001200210111035.dat and 1002200210121035.dat. Do i use AWK or is there some other UNIX command that i can use. Would be very grateful if fellow forummers could highlight a sample code which does what i just mentioned. Thanks in advance.
 
You can use awk but sed would work as well, as would just plain jane shell and cut code.
NOT TESTED.

for xx in targetdir/*
if [ -f "$xx" ] ; then
echo $xx | awk ' {
if ($0 ~ /^ABC_/) {
sbuf = $0
gsub(/ABC/"1001",$0)
cmd = sprintf("%s %s %s", "mv",sbuf,
$0);
system(cmd);
}
}'
fi
done

 
Another way :
for file in ABC_*
do
mv $file `echo $file | sed -e 's/ABC_/1001/g'`
done
;-) Dickie Bird

Honi soit qui mal y pense
 
#!/bin/ksh

oldPREF='ABC_'
newPREF='1002'

for file in ${oldPREF}*
do
mv "${file}" "${newPREF}${file#${oldPREF}}"
done
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top