Assigning file to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning file to a variable
# 1  
Old 06-05-2013
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:

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

error is:

Code:
./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
# 2  
Old 06-05-2013
Modification in your code
Code:
 
for x in /home/dir/*.log
do
 TXTFile=$(basename $x ".log")".txt"
 [ -f $TXTFile ] && cp $TXTFile /home/dir2/
done

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 06-05-2013
Quote:
Originally Posted by brunlea
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.
Code:
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

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 06-05-2013
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?
# 5  
Old 06-05-2013
If there are two file one is ".log" and one is any type of extention then
below code solve your purpose.
Code:
 
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

This User Gave Thanks to pravin27 For This Post:
# 6  
Old 06-05-2013
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?

Code:
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:

Code:
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/.

# 7  
Old 06-05-2013
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)
Code:
for x in *.log; do echo mv ${x%.*}.!(log) /dest/dir; done

Remove the echo if you're happy with the result.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning a variable

I have a date column as 06302015 but I need to have variable which extracts 063015. Am trying something like below but it is not assigning Please let me know if am missing something. Thanks in advance. ################################ #!/usr/bin/ksh DT=06302015 ... (7 Replies)
Discussion started by: weknowd
7 Replies

2. Shell Programming and Scripting

assigning variable in txt file

Hi all, One of my txt file has common format like . And I need to manually assign variable to "/a/b/c/file1/txt" , which has common text before "Calculated summary file:". I wonder if I can use some command to do that for me, that it read the file and check for that comonn text and assign... (2 Replies)
Discussion started by: emily
2 Replies

3. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

4. Shell Programming and Scripting

Assigning a value as a variable from a text file

I have a txt file output.txt Freq = 1900 L = 159I want to assign the values to a variable so that i can further use it in some other script. like F=1900 Len=159 etc i tried doing something with awk but dosent work F=$(awk 'BEGIN {}/Freq/ {split ($2,a);depth=a};printf "%d\t,... (2 Replies)
Discussion started by: shashi792
2 Replies

5. Shell Programming and Scripting

how to Read a file and assigning each line to a variable?

Friends, I have a file output.txt with values as below: 092307135717 061910135717 I want to know how to read this file and then assign each value to a variable. say like var1=092307135717 var2=061910135717 So that I can use this VAR1 and Var2 in the shell script for further processing.... (3 Replies)
Discussion started by: shyamaladevi
3 Replies

6. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

7. Shell Programming and Scripting

Reading data from file and assigning to variable

I was trying to store the number of lines in a file and store it in a file.after that i want to store the information in a file to a variable which is further used in the if loop to check certain condition. #!/bin/bash cat <file> | wc -l > count.txt x="$count.txt"; i=10; if ; then cat... (10 Replies)
Discussion started by: sudhakaryadav
10 Replies

8. UNIX for Dummies Questions & Answers

Searching a text file and assigning it to a variable

Hi Gurus, I am new to unix.I have a requirement as below I have text file like a.txt which contains a.txt hi hello process update status Ok to Proceed no issues good data arrangement My requirement here is i need to read the file and check for the words "OK to Proceed" and if... (2 Replies)
Discussion started by: pssandeep
2 Replies

9. UNIX for Dummies Questions & Answers

Assigning value in a text file to a variable

Hi, I need to place a number located in a text file in a variable so I can perform if/then comparison. How would I go about doing this? Using A=awk '{print $2}' maintenance_date.tmp does not seem to work. Thanks (1 Reply)
Discussion started by: mojoman
1 Replies

10. Shell Programming and Scripting

Reading file and assigning that to Variable

I am missing something here, I have a file which contains only one line and that is either a number or character string. I am trying to read the file and assign that value to a variable and here it seems I am missing something and not getting the expected results... Here is the code : #!/bin/ksh... (2 Replies)
Discussion started by: Vaddadi
2 Replies
Login or Register to Ask a Question