comparing files to contents of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing files to contents of a file
# 1  
Old 06-28-2006
comparing files to contents of a file

Hi I have a problem trying to run a while statement.

I have files under one directory that i need to compare to a value in filex and update that file with the result

files in the directory are

DFC1.
DFC5.
DFC345.
DFC344.
DFC9.

The program i am trying to run will take the number form the filename
and compare it to the value in filex

it should run through each value in the directory compare it to the value in filex and if greater then replace the value in filex and remove the filename from the directory

i.e. compare DFC1. the value 1 to the value in filex = 0
it is greater so replace value in filex with 1 delete DFC1. and do next filename DFC5. same for DFC345. and DFC344 (but this one should fail)

any ideas

trying this #! /usr/bin/ksh

cd /swm/test/apps/creditors/temp
pwd

var1=$(ls -t DFC*.)
echo $var1
#while read var1
#do
echo cat $var1
var2=$(echo $var1|cut -d. -f1|cut -c5-)
echo $var2 > /swm/test/apps/creditors/temp/valuex
if
[$var2 -ge var3=$(cat valuex)]
then
rm $var1
else
echo "the current value is greater than the file $var1

thanks for any help
# 2  
Old 06-28-2006
Helo.
If I've understood you, this would fit your needs:
Code:
#!/bin/bash

# Initial value used to compare.
val=0
echo "initial val=$val"
echo "-----------"

# For each file whose name starts with "DFC" and is under files/ folder do
for f in $(ls -1 files/DFC*); do
   echo "File: $f"
   # get the number from the filename
   num=$(echo $(basename $f) | cut -d. -f1|cut -c4-)
   echo "num: $num"
   # Compare it to val value
   if [ $num -gt $val ]; then
      # It's greater, so update val value and delete file.
      val=$num
      rm $f
      echo "${f} deleted."
   fi
   echo "val=$val"
   echo "--"
done

echo "-----------"
echo "###########"
echo "-----------"
echo "val=$val"

For bash, but most probably will work under ksh.

The script assumes your files are stored in ./files

Regards.
# 3  
Old 06-28-2006
Cheers grial almost there only problem keeps reverting to $val to 0

I need to keep a record of the last value and use this the next time the process is ran
# 4  
Old 06-28-2006
You could store it in a file:
Code:
# Initial value used to compare.
val=$(cat /path/to/file.txt)

/path/to/file.txt
should contain only one line with the number and must exist in the first run in this case.
Finally:
Code:
echo "-----------"
echo "###########"
echo "-----------"
echo "val=$val"
echo $val > /path/to/file

So that you update the file with the new value. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

Help with comparing contents of a file

I have a script that outputs a file in the below format group1:user1,user2,user3, user4 group2:user2, user4, user5 group3: user1, user6, user7 If there is a change in /etc/passwd with addition/deletion of users, then the next run of the script will output the file with the changes. How... (3 Replies)
Discussion started by: Newbie2015
3 Replies

3. Shell Programming and Scripting

Need Script to copy the contents of two files into one file

Hi i need Script to copy the contents of two files into one file i have 2 fil X1.txt / X2.txt i need script to copy the contents of X1 and X2 In AllXfile X1.txt File X1 X2.txt File X2 AllXfile.txt File X1 File X2 (2 Replies)
Discussion started by: azzeddine2005
2 Replies

4. Shell Programming and Scripting

Comparing two files and creating a new file

Hi, I want to compare two files based on the data in their first column. Both the files are not equal in their number of columns and number of entries or rows. The entries (only the first column) of the file1 should be compared with the entries (only the first column) of the file2. If the... (3 Replies)
Discussion started by: begin_shell
3 Replies

5. Shell Programming and Scripting

Need Help with Bash - comparing directory contents with list of files

Hey guys, need help with a script I'm trying to write. Basically I need to compare the contents of a folder called "profiles" with a list of files called "template". when the file matches the contents of the folder it needs to set a variable called "checked" to "1" Cookies to anyone... (4 Replies)
Discussion started by: Scriporium
4 Replies

6. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

7. Shell Programming and Scripting

How to mix the contents of 2 files into a new file?

Hello Everybody! My question is how can I mix for example file_a with file_b in the following method: After 2 lines of file_a put 2 lines from file_b to file_c. For example: file_a: 1 2 3 4 5 6 file_b: 11 22 (7 Replies)
Discussion started by: Levi85
7 Replies

8. Shell Programming and Scripting

Compare two files and remove all the contents of one file from another

Hi, I have two files, in which the second file has exactly the same contents of the first file with some additional records. Now, if I want to remove those matching lines from file2 and print only the extra contents which the first file does not have, I could use the below unsophisticated... (3 Replies)
Discussion started by: royalibrahim
3 Replies

9. Shell Programming and Scripting

How can i prepare a file by comparing two other files?

File 1 data: TestA TestB TestC File 2 data: TestA TestD TestE My Output File (pick all from both and create a file without duplicate data) ---------------------------------------------------------------------- TestA TestB TestC (3 Replies)
Discussion started by: manmohanpv
3 Replies

10. Shell Programming and Scripting

Comparing contents of files

Hi, I hav two files a1.txt and a2.txt, a1.txt contains: --------------- asdev ebcdev .... a2.txt contains: --------------- asdev ebcdev prod .... a1.txt will be updated by a process,.. now i want to compare two files and i want to see data which is not in a1.txt am i clear....?? ... (3 Replies)
Discussion started by: rrs
3 Replies
Login or Register to Ask a Question