Need a script that will check for changes in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a script that will check for changes in a text file
# 1  
Old 12-26-2009
Need a script that will check for changes in a text file

Hi Everyone,

Can someone please show me how to create a script that will search a text file for changes.

For example below is a sample of how the text file would look like,

RLH rlh-1 ALIVE 20:06:05
RLH rlh-7 ALIVE 20:06:05
RLH rlh-3 ALIVE 20:06:05
RLH rlh-2 ALIVE 20:06:05

I would like the script to alert me on screen when the "ALIVE" word for a particular line say rlh-1 changes to something else.

Thanks a lot.
# 2  
Old 12-26-2009
Hello
you can create a backup of your existing file. can you use "diff" in a cron job to check the file against the backup file. If there is some difference then the cron print to say your mailbox a diagnostic message. After the change you can mv current_file backup_file to see further changes.
Regards.
Code:
cp file file.backup

cron job
Code:
#!/usr/local/bin/perl
use strict;
use warnings;
my @arr=`diff /path/to/file /path/to/file.backup`;   #store the difference of lines
if(@arr) {foreach (0..$#arr)                                  #check if the difference exists
{print $arr[$_];} 
my$res=`mv /path/to/file /path/to/file.backup`}      #change the backup file to be the current file (if wanted,otherwise not)

Regards.

Last edited by gaurav1086; 12-26-2009 at 04:05 PM..
# 3  
Old 12-26-2009
Thank you gaurav1086,

The script below may help explain what I have in mind.

HTML Code:
if grep -e rlh-1 chkstat.txt | awk '{print $4}' == "$ALIVE"
then
    > /dev/null
else
    echo "RLH -1 service is not running"
    echo "$beep"
fi
The if statement is supposed to check whether the output equals the string "$ALIVE" and if it does not, then display the string "RLH -1 service is not running"

Obviously it does not work because it is giving me the error below

awk: cmd. line:1: fatal: cannot open file `==' for reading (No such file or directory)

Please advice.
# 4  
Old 12-26-2009
Perhaps the following will suit your needs:

Code:
if ! awk '$2=="rlh-1" && $3!="ALIVE" {exit 1}' chkstat.txt; then
    echo "RLH -1 service is not running"
    echo "$beep"
fi

# 5  
Old 12-27-2009
why not this.
Code:
  awk -v ALIVE=$var '$4!=$ALIVE{system("echo $beep");print "RLH-1 service is not running"}' in_file

where $var is the shell variable that you want to pass.
Regards.
# 6  
Old 12-27-2009
Hi, guarav1086:

Quote:
Originally Posted by gaurav1086
why not this.
Code:
  awk -v ALIVE=$var '$4!=$ALIVE{system("echo $beep");print "RLH-1 service is not running"}' in_file

where $var is the shell variable that you want to pass.
Regards.
From kumaran21's original post and the later post's grep, it seems that a specific line's status is being tested (in the sample data provided, those with $2="rlh-1"). Your code will trip on any.

Unless it's being exported into the environment, $beep will be undefined when awk executes a subshell to run "echo $beep" (running a shell to simply echo is rather wasteful; awk's print statement will do). $beep is also undefined within the scope of the awk script, but it can be properly passed in via -v on the command line or expanded by the shell itself before invoking awk if the quoting is adjusted.

I don't think parameterizing the "ALIVE" field would be as useful as the "rlh-??", but I can't say since i don't know exactly how the script will be put to use.

Take care,
alister

Last edited by alister; 12-27-2009 at 07:13 AM..
# 7  
Old 12-27-2009
Quote:
Originally Posted by alister
Hi, guarav1086:



From kumaran21's original post and the later post's grep, it seems that a specific line's status is being tested (in the sample data provided, those with $2="rlh-1"). Your code will trip on any.

Unless it's being exported into the environment, $beep will be undefined when awk executes a subshell to run "echo $beep" (running a shell to simply echo is rather wasteful; awk's print statement will do). $beep is also undefined within the scope of the awk script, but it can be properly passed in via -v on the command line or expanded by the shell itself before invoking awk if the quoting is adjusted.

I don't think parameterizing the "ALIVE" field would be as useful as the "rlh-??", but I can't say since i don't know exactly how the script will be put to use.

Take care,
alister
Hello alister,
He mentioned to test $4(field) so I dint consider other field as I do not know about the script being affected by other parameters.. I agree $beep would not produce any significant result. But I guess $beep is not an "alarm" . I guess he has mentioned it to signify that some handler is to be invoked in respose to this event, but I am not sure. Again if its not like that , then $beep can be very well passed as "-v" parameter. Anyways, thanks for pointing it out.
Regards
Gaurav.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if files inside a text file are found under a directory

Hi all, Please somebody help me with this: I want to check if the files listed in a text file, are found under a directory or not. For example: the file is list_of_files.txt, which contains inside this rows: # cat list_of_files logs errors paths debug # I want to check if these... (3 Replies)
Discussion started by: arrals_vl
3 Replies

2. Shell Programming and Scripting

Check if the text file has more than 2 characters

Guys, I know that the below command will cut the 13th field from test.txt file awk -F"|" '{print $13}' test.txt The answer would be, CA CN Ohio If we see the 3 rd one, it has more than 2 characters. So i wanted to check this in if condition and i want to get the output if the 13th... (4 Replies)
Discussion started by: AraR87
4 Replies

3. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

4. Shell Programming and Scripting

Shell program to check if the same text appears twice in an XML file

Hi All, I am very new to this forum and beginner to shell scripting. I need a shell script to: Search for a text in XML file à if the same text appears twice in an XML file à output file name Script should loop thru every xml file of a given folder. Please help me writing this script. ... (1 Reply)
Discussion started by: amardeep001
1 Replies

5. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

6. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

7. Shell Programming and Scripting

Check if a text file is empty or not (using ls -s)

Hello, I want to make a script which says if a text file is empty or not. I tried two ways of making it, but I have problems with both of them. Now I think that the better way is the ls -s solution (considering that an empty text file has a 0 weight, because "cat file.txt" fails when file is... (4 Replies)
Discussion started by: Link_
4 Replies

8. Shell Programming and Scripting

how can i check if text file is closed ?

Hello all im trying to make daemon that listen for incoming textiles and then it will move them to defined directory now the daemon part is ok but im stack on how to check of file is close and done copied to directory . so i could process it and move it forward tnx (13 Replies)
Discussion started by: umen
13 Replies

9. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies

10. UNIX for Dummies Questions & Answers

How to check if a unix text file is being accessed?

I am writing a script that periodically reads in data from a text file. The only issue is that, that text file is periodically updated (appended to) by another script. I am using perl in UNIX environment. How can I check if that text file is being accessed, so I can wait until it is no longer being... (6 Replies)
Discussion started by: rickylui
6 Replies
Login or Register to Ask a Question