Searching for variable string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for variable string
# 1  
Old 09-08-2015
Searching for variable string

Hi Guys,

So im trying to search for the most recent occurance of a string in the most recently updated .log file in a certain directory. The string i am searching for is a value, now if this value is greater than 800 i would like an email sent out with some text (blah blah blah).

This is what i have so far

Code:
 
ls -Art | tail -n 1| xargs tac | grep -m1  "Total number of clients: "

Running this command will output the following
Code:
19:55:51.815 Total number of clients: 338
xargs: tac: terminated by signal 13

As you can see the total number of clients is smaller than 800, so we dont need an email notification sent out. But in the instance of the value being greater than 800 we will need a notifcation sent.

Your help is appreciated.

Moderator's Comments:
Mod Comment Please use next time code tags for your code or data, thanks

Last edited by vbe; 09-08-2015 at 05:17 AM.. Reason: code tags
# 2  
Old 09-08-2015
Try this using awk:
Code:
fname=$(ls -Art | tail -n 1) 
awk  '/Total number of clients: / && $(NF)>800 {print $0}' $fname > tmp.tmp
[ -s tmp.tmp ] && mailx -s 'blah blah' me@mycompany.com < tmp.tmp

# 3  
Old 09-08-2015
For "the most recent occurance", you might want to adapt jim mcnamara's proposal like
Code:
awk  '/Total number of clients: / && $(NF)>800 {T=$0} END {print T}' $fname > tmp.tmp

# 4  
Old 09-09-2015
Quote:
Originally Posted by jim mcnamara
Try this using awk:
Code:
fname=$(ls -Art | tail -n 1) 
awk  '/Total number of clients: / && $(NF)>800 {print $0}' $fname > tmp.tmp
[ -s tmp.tmp ] && mailx -s 'blah blah' me@mycompany.com < tmp.tmp


I need to make this as a cronjob that runs every 3 minutes however, this seems to send an email out regardless if the value is larger or smaller than 800. I need it ONLY to email if the value is larger than 800, any ideas?


Cheers

Last edited by hello_world; 09-09-2015 at 04:08 AM..
# 5  
Old 09-09-2015
What operating system are you using?

Are you specifying the shell to be used in your script? (If so, how?)

When you are running your script with cron, how are you setting your script's environment variables and the directory in which your script is run?

In what directory are you hoping to find the file(s) you want to process?
# 6  
Old 09-10-2015
Quote:
Originally Posted by Don Cragun
What operating system are you using?

Are you specifying the shell to be used in your script? (If so, how?)

When you are running your script with cron, how are you setting your script's environment variables and the directory in which your script is run?

In what directory are you hoping to find the file(s) you want to process?
* Linux 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

*I created a shell script and gave it execute access E.g. ./connectioncount.sh

*I havent set up the cron yet, once i get the shell script working i will implement as a cronjob

*The directory is in Genesyslogs\confserv

Hope that answers your questions.


Reading in, ive decided to rewrite the script as i prefer to use tac rather than awk as the file is quite large and the job will need to run every 3 minutes. Im half way there, Please see below

Code:
#!/bin/bash
fname=$(ls -Art | tail -n 1)
echo $fname
value=$(tac $fname | grep -m1 "Total number of clients:")
echo $value


All i need now is to capture the last numeric values which appear after Total number of clients: xxxx

If this value is greater than 800 than an email will be sent out.


Thanks.

---------- Post updated at 10:25 PM ---------- Previous update was at 10:24 PM ----------

Almost there, though i cannot seem to get the if statement working, it hangs on that particular command and i dont receive an email. Did i use the correct syntax?

Code:
#!/bin/bash
fname=$(ls -Art | tail -n 1)
echo $fname
value=$(tac $fname | grep -m1 "Total number of clients:")
echo $value
variable=${value##* }
echo $variable
if [ $variable -lt 800 ]; then
	mailx -s "vpsi1ftc has exceeded the connection: $variable" mycompany@company.com.au
fi


Last edited by Don Cragun; 09-10-2015 at 01:25 AM.. Reason: Add CODE and ICODE tags, again.
# 7  
Old 09-10-2015
Quote:
Originally Posted by hello_world

---------- Post updated at 10:25 PM ---------- Previous update was at 10:24 PM ----------

Almost there, though i cannot seem to get the if statement working, it hangs on that particular command and i dont receive an email. Did i use the correct syntax?

Code:
#!/bin/bash
fname=$(ls -Art | tail -n 1)
echo $fname
value=$(tac $fname | grep -m1 "Total number of clients:")
echo $value
variable=${value##*:  } # there's an space before close }
echo $variable
if [ "$variable" -lt 800 ]; then
	mailx -s "vpsi1ftc has exceeded the connection: $variable" mycompany@company.com.au

fi

Take a look at the red highlighted. If your shell supports it you may do
Code:
if [[ variable -lt 800 ]]

or
Code:
if (( variable < 800 ))


However you have been mentioning all along:
Quote:
If this value is greater than 800 than an email will be sent out.
Thus, the proper comparison should be:
Code:
if [ "$variable" -gt 800 ]


Last edited by Aia; 09-10-2015 at 12:53 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

searching a variable in environment

all, i am trying to search a variable in unix account. variable TEST_LEVEL=500 , i am getting this value if i grep in env .if i try to see the file where it is defined , i do get 1 file . i commented thsi variable in that file and did relogin, but still i see TEST_LEVEL=500 in thye... (3 Replies)
Discussion started by: deepakiniimt
3 Replies

2. Shell Programming and Scripting

Searching a String

Hi everyone ! suppose i'm searching for a specific string in a file so it is very easy, i use the following command grep 'keyword' file_name but how to search a word which is repeated maximum number of times in a file, for example in the following text i have to search a word which is... (12 Replies)
Discussion started by: ourned
12 Replies

3. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

4. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

5. Shell Programming and Scripting

sed - searching for string and storing in variable

Hi I'm trying to find a way to search a text file for a specific string. I have a file which contains i.p. addresses and port numbers in the following format: 'ip="www.xxx.yyy.zzz"' 'port="xx""' I want to print only the parts between the double quotes for use in seperate variables,... (4 Replies)
Discussion started by: melias
4 Replies

6. UNIX for Dummies Questions & Answers

Searching for a string variable

Hi ... I have a string variable STR = "This is a test message" I have a file abc.txt that I am searching for the occurence of the string STR ... I am using the command in a script cat abc.txt | grep $STR It identifies each space as a seperator and prints word by word. How to... (2 Replies)
Discussion started by: mattrix
2 Replies

7. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

8. Shell Programming and Scripting

one more query for searching string.......

Dear friends, I have one more query, incase a file contains multiple tags of same name, then how to get the required string between the tags, in which the string begins with "O/M" i.e., file1.txt contains following text(please note that all the following tags are in single line)... (6 Replies)
Discussion started by: swamymns
6 Replies

9. Shell Programming and Scripting

Searching a string for a character

I have a co-worker that is trying to make a Wheel of Fortune game and he wants to know if there is a way to search a string for the letter given by the user. (7 Replies)
Discussion started by: turbulence
7 Replies

10. UNIX for Dummies Questions & Answers

searching for a string in directory

Hi, I have a directory with a couple of thousand logs in it. The log files are created every 5 minutes. I want to search these logs grep for a specific sting and more importantly print the name of the files where that sting was found. e.g. all logs begin om20020927 what I have been... (4 Replies)
Discussion started by: warrend
4 Replies
Login or Register to Ask a Question