Assigning file to a variable

Hi,

I have a list of files in a directory. Each file has a .txt and a .log extension i.e. file.txt & file.log, file1.txt & file1.log etc. The file with the .log extension may not always exist alongside the file with the .txt extension.
I need to copy the .txt file if there is a corresponding .log file with it. If there is no .log file, then do not do the copy. Once the file has been copied i need to delete it.

I am using ksh on a solaris server.
I have tried the code below to initially assign each file with a .log extension to a variable but i am getting an error:

i=0
for x in /home/dir/*.log
do
arr[$i]=$x
i=`expr $i + 1`
done
echo "${arr[@]}"

error is:

./script.sh arr[0]=/home/dir/file.log: not found
./script.sh arr[1]=/home/dir/file1.log: not found

After this i was going to then cut the .log in the filename and do a search with the .txt extension and then copy it. Can't get the first bit to work though.

Any ideas how i can solve this problem?

Thanks

Modification in your code

 
for x in /home/dir/*.log
do
 TXTFile=$(basename $x ".log")".txt"
 [ -f $TXTFile ] && cp $TXTFile /home/dir2/
done
1 Like
for txtfile in /home/dir/*txt
do
    filename=${txtfile%.*}
    filename=${filename##*/}
    
    if [ -e "${filename}.log" ]
    then
        cp $txtfile /home/newdir/
        rm -f $txtfile
    fi
done
1 Like

Thanks for both them solutions. However, i need them modifying slightly. There will always be corresponding .log file, but the other file may have some other extension that is not known, so it could be .txt, .not, .doc, .ntp etc. Is there any way to account for this in the code?

If there are two file one is ".log" and one is any type of extention then
below code solve your purpose.

 
for x in /home/dir/*.log
do
 TXTFile=$(basename $x ".log")
 [ -f $TXTFile.[^log]* ] && cp $TXTFile.[^log]* /home/dir2/raid
 rm -f $TXTFile.[^log]*
done
1 Like

That's brilliant. Thanks

---------- Post updated at 08:41 AM ---------- Previous update was at 08:00 AM ----------

I am trying to do the copy all in one go rather than one file at a time, by assigning the file name to an array. Is this possible?

i=0
for x in /home/dir/*.log
do
TXTFile=$(basename $x ".log")
filename=$TXTFile.[^log]
[ -f $filename ] && filearr[$i]=$filename
i=$(($i+1))
done
cp ${filearr[@]} /home/dir/folder/.

With the above code i am not able to get the correct filename into $filename. It always appears as file1.[^log], file2.[^log] etc.

Any way i can get it into an array?

---------- Post updated at 09:17 AM ---------- Previous update was at 08:41 AM ----------

ok, i think i've got it:

i=0
for x in /home/dir/*.log
do
TXTFile=$(basename $x ".log")
filename=`find /home/dir/ -name ""$TXTFile".*"|grep -v .flg`
[ -f $filename ] && filearr[$i]=$filename
i=$(($i+1))
done
cp ${filearr[@]} /home/dir/folder/.

Don't rm after cp without error checking! Should your cp fail, your file is lost. And, that [^log] might not work. If you've got bash, try (with extglob option set)

for x in *.log; do echo mv ${x%.*}.!(log) /dest/dir; done

Remove the echo if you're happy with the result.