I've been trying to automate some processes with various Windows tools. I found that using unix scripts the result would be closest to my needs. So I installed Cygwin on Windows 7.
My folders and files are structured like this:
folder1
-file1_temp.txt
-file2_temp.txt
folder2
-file1_temp.txt
-file2_temp.txt
The desired outcome should look like this:
folder1
-file1_folder1.txt
-file2_folder1.txt
folder2
-file1_folder2.txt
-file2_folder2.txt
Now, here's what's bothering me:
I have a loop script for renaming files which contain the "temp" string for the current opened folder:
for i1 in *temp.*
do
i2=`echo $i1 | sed 's/temp/<FOLDER_NAME>/g'`
mv $i1 $i2
done
I want to automate the process of renaming files and replacing "temp" string in each file with the current folder name, but I'm failing to find a command which would automatically find the current folder name and put it in sed 's/temp/<FOLDER_NAME>/g'`.
I would also like to automate the above loop so that it could be applied to all folders (folder1, folder2, etc).
Try this...
The script is placed in the parent directory where the subfolders are present and ths script name is "run.sh"
#!/bin/ksh
HOMEDIR=/user/ahamed/test
dirs=`ls | grep -v run`
for dir in $dirs
do
cd $HOMEDIR/$dir
files=`ls`
dirname=`echo $dir | sed 's=/==g'`
for file in $files
do
newfile=`echo $file | sed "s/temp/$dirname/g"`
mv $file $newfile
done
done
#!/bin/ksh
HOMEDIR=/cygdrive/n/test
dirs=`ls | grep -v run`
for dir in $dirs
do
cd $HOMEDIR/$dir
files=`ls`
dirname=`echo $dir | sed 's=/==g'`
for file in $files
do
newfile=`echo $file | sed "s/temp/$dirname/g"`
mv $file $newfile
done
done
I have one other script with grep and sed commands which creates the mentioned above temp files. It looks like this:
grep '.*' *.txt > merged_temp.txt #merging all text files in the opened subfolder to one file
sed -i 's/.txt:/ /g' merged_temp.txt #trimming the merged file for better looks
Is there a way to join the above two scripts together, so that the script would execute the command in the following order for every subfolder in the main directory:
enter first subfolder and execute the grep and sed commands, creating the merged file in the current folder;
rename the merged file in the first subfolder to the name of the subfolder;
If you are using the grep and sed command to merge all the file in the subfolder to one big file, then you can try this
#!/bin/ksh
HOMEDIR=/cygdrive/n/test
dirs=`ls | grep -v run`
for dir in $dirs
do
cd $HOMEDIR/$dir
files=`ls`
dirname=`echo $dir | sed 's=/==g'`
for file in $files
do
echo $file >> merged_$dir.txt #this will have the file name first
cat $file >> merged_$dir.txt # and then the contents.
newfile=`echo $file | sed "s/temp/$dirname/g"`
mv $file $newfile
done
done
#!/bin/ksh
HOMEDIR=/cygdrive/n/test
dirs=`ls | grep -v run`
for dir in $dirs
do
cd $HOMEDIR/$dir
files=`ls`
dirname=`echo $dir | sed 's=/==g'`
for file in $files
do
grep '.*' *.txt > _merged_temp.txt
sed -i 's/.txt:/ /g' _merged_temp.txt
newfile=`echo $file | sed "s/temp/$dirname/g"`
mv $file $newfile
done
done
But the above script creates the merged file in the main directory, not in the subfolder where it needs to be And if there's another folder, it copies it into the selected subfolder.
Doh.. it's getting complicated... I started this yesterday. Be easy on me