Using below test.sh, I am getting result in dump.txt as below:test.sh:#!/bin/bash#Maxl-script zone starts.....essmsh -l admin password -s localhost -i << EOFspool on to 'dump.txt';display session all;spool off;EOF#Maxl-script zone ends.....dump.txt:user                session             login_time          application         database            db_connect_time     request             request_time        connection_source   connection_ip       request_state+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+------------------- admin                                 0                   9                                                           0 none                                  0 Not Requested*      a00:bf32::                     admin                         989855740                1335 DRRDEVMH            DRRPRODB                           1201 none                                  0 Not Requested       a00:8a45::                     admin                        1768947706                 932 test                test                                916 none                                  0 Not Requested       a00:94b6::                     WARNING - 1241024 - Possible string truncation in column 1. WARNING - 1241028 - Output column defined with warnings. WARNING - 1241024 - Possible string truncation in column 9. WARNING - 1241028 - Output column defined with warnings. WARNING - 1241024 - Possible string truncation in column 10. WARNING - 1241028 - Output column defined with warnings. OK/INFO - 1241044 - Records returned: [3].As we can see in the last line of dump.txt there is a string Records returned: [3]. That digit 3 is my target, I want to send a email notification to users having following Lines as Email body:Total Number of Sessions running = 3 NOTE: This is an automatically generated email, please don't reply to it.After Googling, I have managed to get below method to get it done.Can anybody please help me out for how to use sed or any other method to meet my objective in above script?$ var=Records returned :echo Total Number of Sessions running = $var | sed 's/.*://'  > file.tmp echo -e \n >> file.tmp echo NOTE: This is an automatically generated email, please don't reply to it.  >>   file.tmp fimailx -s Subject emailaddresses < file.tmprm file.tmpNOTE: Records Returned... line will be always the last line in dump.txt.
Mailing out a customised content based on specific search in a logfile
shell script;text processing;sed;logs;search
You could also use grep and a positive lookahead:$ grep -oP '(\d+)(?=\].s*$)' dump.txt 3The regular expression above searches for the longest string of numbers followed by ]. and 0 or more spaces until the end of the line. You can combine this with the rest of your email message like so:$ read -r -d '' message <<'EOF'> Total Number of Sessions running = $(grep -oP '(\d+)(?=\].\s*$)' dump.txt)> > NOTE: This is an automatically generated email, please don't reply to it.> EOF$ echo $message | mailx -s Subject emailaddressesTo get that right, type read -r -d '' message <<'EOF' and hit Enter. Then, paste or type in Total Number of Sessions running = $(grep -oP '(\d+)(?=\].\s*$)' dump.txt) hit Enter again (as many times as you want blank lines), paste the rest of the message and write EOF to exit.