The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can i prepare a file by comparing two other files? manmohanpv Shell Programming and Scripting 3 02-18-2008 04:58 AM
Comparing contents of files rrs Shell Programming and Scripting 3 04-03-2007 08:08 PM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-09-2007 02:38 AM
Creating file contents using contents of another file ReV Shell Programming and Scripting 21 02-24-2006 10:25 AM
shell script comparing files in a file raina_nalin Shell Programming and Scripting 4 06-21-2005 06:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-28-2006
SummitElse SummitElse is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 19
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 (permalink)  
Old 06-28-2006
grial's Avatar
grial grial is offline Forum Advisor  
El UNIX es como un toro
  
 

Join Date: Jun 2006
Location: Madrid (Spain)
Posts: 531
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 (permalink)  
Old 06-28-2006
SummitElse SummitElse is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 19
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 (permalink)  
Old 06-28-2006
grial's Avatar
grial grial is offline Forum Advisor  
El UNIX es como un toro
  
 

Join Date: Jun 2006
Location: Madrid (Spain)
Posts: 531
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0