I have some files, those are abbreviated (ed,ea, and bi)
company_ed_20100719.txt
company_ea_20100719.txt
company_bi_20100719.txt
I would like to rename these files by replacing
ed with EmployeeDetails
ea with EmployeeAddress
bi with BankInfomration
as
company_ EmployeeDetails _20100719.txt
company_ EmployeeAddress _20100719.txt
company_ BankInfomration _20100719.txt
I want to write a shell script(ksh) that does the following
if [ file name contains �ed' ] then
Replace ed with EmplaoyeeDetails
else if [file name contains �ea' ] then
Replace ea with EmplaoyeeAddress
else if [file name contains �bi' ] then
Replace bi with BankInformation
fi
I am not sure what is the command/utility that helps my to serve similar to strstr function?
Backup your data first!
Assuming an old ksh with no associative arrays:
for f in *_*_*.txt; do
fp=${f%%_*} rst=${f##*_}
case $f in
*_ed_* ) mv -- "$f" "$fp"_EmployeeDetails_"$rst";;
*_ea_* ) mv -- "$f" "$fp"_EmployeeAddress_"$rst";;
*_bi_* ) mv -- "$f" "$fp"_BankInfomration_"$rst";;
esac
done
it will take files 1 by 1 from your current directory and do the search and neccesary changes.
for filename in `ls *`
do
if [`grep ea "filename"`]
echo filename |sed 's/ea/EmployeeAddress/'
elif [`grep ed "filename"`]
echo filename | sed 's/ed/EmployeeDetails/'
elif [`grep bi "filename"`]
echo filename | sed 's/bi/BankingInformation/'
fi
done
but i will still prefer the case code that radulov has displayed above. its more robust than mine.
#!/bin/bash
my=("ed" "ea" "bi")
newmy=("EmployeeDetails" "EmployeeAddress" "BankInfomration")
ix=0
for i in ${my[@]}
do
myfile=$(ls -1 company* | grep $i)
newfile=$(echo $myfile | sed "s/^\(.*_\).*\(_.*\)/\1 ${newmy[ix]} \2/")
mv -v "$myfile" "$newfile"
((ix++))
done