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
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-21-2007 01:29 AM
appending to sed output of one file into the middle of file go4desperado Shell Programming and Scripting 5 02-05-2007 02:20 AM
Appending data at the first and last line of a file brainstormer Shell Programming and Scripting 4 01-03-2007 10:38 AM
Capturing shell script command output designflaw Shell Programming and Scripting 2 03-01-2006 04:24 PM
capturing output in script MizzGail Shell Programming and Scripting 6 06-02-2004 07:44 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 08-30-2007
wally_welder's Avatar
wally_welder wally_welder is offline
Registered User
  
 

Join Date: Aug 2007
Location: Dongen, Netherlands
Posts: 2
capturing line from script output and appending to a file

Hi all,
I did some searching in this forum but can't find anything that matches the issue I'm bumping heads with.

On a CentOS4/Postfix (and bash everywhere) mail gateway box I run a command periodically to purge the Postfix queue of messages "From:MAILER-DAEMON".

This is the one line'r (courtesy of the Postfix list archives)

Code:
mailq | grep -v "^[^0-9A-Z]+" | grep MAILER-DAEMON | awk '{print $1}' | postsuper -d -
This generates output exactly as shown:
[many preceding lines of exactly the same format]
postsuper: E657736C82BD: removed
postsuper: E1CAD36C8074: removed

and finally, at the end of the output:
postsuper: Deleted: 127 messages

What I'm trying to figure out how to do is this.....
Grab the number of deleted messages, and append them to a file with a timestamp e.g. 20070830 07:32 - 127

so somewhere in the latter end of this would be >> cron_purges

Can anyone help me understand how to grab *just* the number, and hang a timestamp onto it as shown in the above example?

Regards & TIA!
Wally
  #2 (permalink)  
Old 08-30-2007
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
one way

One way would be to get pipe the o/p and filter out the last line and get the no of deleted messages from there & assign it to a variable.
Code:
no_of_msgs=$(mailq | grep -v "^[^0-9A-Z]+" | grep MAILER-DAEMON | awk '{print $1}' | postsuper -d - | tail -1 | awk '{print $3}')
Format the date to get the date-time stamp required
Code:
dt_time=$(date +'%Y%m%d %H:%M')
Now echo this to a file where you want to log this.
Code:
echo "$dt_time - ${no_of_msgs}" >>cron_purges
  #3 (permalink)  
Old 08-30-2007
wally_welder's Avatar
wally_welder wally_welder is offline
Registered User
  
 

Join Date: Aug 2007
Location: Dongen, Netherlands
Posts: 2
capturing line from script output and appending to a file

Quote:
Originally Posted by ranj@chn View Post
One way would be to get pipe the o/p and filter out the last line and get the no of deleted messages from there & assign it to a variable.
Code:
no_of_msgs=$(mailq | grep -v "^[^0-9A-Z]+" | grep MAILER-DAEMON | awk '{print $1}' | postsuper -d - | tail -1 | awk '{print $3}')
Format the date to get the date-time stamp required
Code:
dt_time=$(date +'%Y%m%d %H:%M')
Now echo this to a file where you want to log this.
Code:
echo "$dt_time - ${no_of_msgs}" >>cron_purges
That does some rather interesting things:
1. It does run the deletions successfully, but outputs verbosely to screen in the original format
2. It does write a timestamp to the file cron_purges
but
3. Doesn't capture the number (represented as "xxx" below) appearing in the last line of output:
postsuper: Deleted: xxx messages

Unfortunately, I don't know enough about the syntax to mess around with possible alternatives in order to offer a suggestion...so, I'm a bit stumped still.

Regards & Thanks!
-Wally
  #4 (permalink)  
Old 08-30-2007
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
pipe may not be working

I am not sure what exactly is the reason, but the o/p of postsuper -d seems to be not piped to tail command. Try this,
Redirect the o/p of postsuper -d to some tempfile. Get the count of messages from the tempfile.
Code:
mailq | grep -v "^[^0-9A-Z]+" | grep MAILER-DAEMON | awk '{print $1}' | postsuper -d - >tempfile
no_of_msgs=$(tail -1 tmpfile |awk '{print $3}')
The rest being the same. Remove the tempfile afer the work is over. If this is not working, then do check if the last line is a blank line in the tempfile. Empty lines at the end could also distort the o/p. But I donot know how postsuper works to clearly identify the cause. Hope that helps!!
  #5 (permalink)  
Old 08-30-2007
fazliturk fazliturk is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 45
try this;

NumOfMsg=`.....(your orijinal code) ...|grep Deleted |awk '{print $3}' `
I thing tail is not suitable for pipe why pipe sends line by line so for tail -1 every line will be the last line
  #6 (permalink)  
Old 08-31-2007
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
tail should work

Quote:
fazliturk
I thing tail is not suitable for pipe why pipe sends line by line so for tail -1 every line will be the last line
tail should work. Check this,
Code:
rkumar@bdc4reteaix1w: /home/rkumar >ls
bkgdtest         dead.letter      mbox             mypipe           nohup.out        test.pl          unix_forum
cprog            f1.txt           myfile           newone           restart_sshd.sh  testpipe.sh      whsmith
rkumar@bdc4reteaix1w: /home/rkumar >ls | tail -1
whsmith
  #7 (permalink)  
Old 08-31-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
Code:
mailq | awk '!/^[^0-9A-Z]+/ && /MAILER-DAEMON/{
     cmd = "postsuper -d " $1
     cmd | getline #or system(cmd)
     close(cmd) 
}'
NB: Not tested.
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 09:18 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