capturing line from script output and appending to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting capturing line from script output and appending to a file
# 1  
Old 08-30-2007
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  
Old 08-30-2007
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  
Old 08-30-2007
capturing line from script output and appending to a file

Quote:
Originally Posted by ranj@chn
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  
Old 08-30-2007
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  
Old 08-30-2007
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  
Old 08-31-2007
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  
Old 08-31-2007
Code:
mailq | awk '!/^[^0-9A-Z]+/ && /MAILER-DAEMON/{
     cmd = "postsuper -d " $1
     cmd | getline #or system(cmd)
     close(cmd) 
}'

NB: Not tested.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending output to file

Hi, I want to calculate std dev for a list of files and then print the output appended to an existing file. I have a few folders within the directory Folder, and I am interested in getting the std dev of values in files named as time.txt. In the last pipe, if I print just the sd value, it... (1 Reply)
Discussion started by: jamie_123
1 Replies

2. Shell Programming and Scripting

Help capturing output of expect script

match_max 500000 set timeout 30 set outcome1 {} set outcome2 {} set inputfile C:\\Users\\Administrator\\Desktop\\inputfile.txt send -i $con "\r"; expect -i $con "Desktop>" { exp_send "type $inputfile \r" } set timeout 30 expect { "Desktop>" { set outcome $expect_out(0,string);}... (3 Replies)
Discussion started by: cityprince143
3 Replies

3. Shell Programming and Scripting

Appending standard C Time on each line out output

I have a command : nawk 'BEGIN{print srand()}' which gives the amount of seconds between January 1st 1970 00:00:00 (Unix Epoch) and the present time, to the closest second and I would want to append this time stamp every minute in a file which contains the data below which also has one row... (1 Reply)
Discussion started by: thinktank
1 Replies

4. Shell Programming and Scripting

Capturing script output and input to log

Hi Is there a way that I can capture a shell script (both output and input) to a log file where I can analyze it? Thanks (6 Replies)
Discussion started by: nimo
6 Replies

5. Shell Programming and Scripting

Appending script output to file

Hi guys, I have a script from which I would like its standard output results (upon execution) to be appended to a separate file (.txt format). How can I write such a command into the script for this to occur? I know there should be a >> involved somewhere. Thanks! (5 Replies)
Discussion started by: jjb1989
5 Replies

6. Shell Programming and Scripting

Capturing output of procedure in variable in shell script

Hi guys I am calling one DB2 stored proc through unix. It is giving me below output. I want to capture the value 150 in one UNIX variable in shell script. Please let me know how I can achieve this. Thanks in advance Value of output parameters -------------------------- Parameter Name :... (5 Replies)
Discussion started by: vnimavat
5 Replies

7. Shell Programming and Scripting

Problem capturing output in TCL script

I have a TCL script that logs into a switch using expect.I send a command "show port-security address" and it returns a table having a large number of rows.I need to capture this output(the table) and store it in a .txt file. I have done this: match_max 5000 set expect_out(buffer) {} set... (0 Replies)
Discussion started by: plasmalightwave
0 Replies

8. Shell Programming and Scripting

Capturing Sybase SP output in Shell Script

Greetings, I need to capture the output of a Sybase stored procedure, inside my shell script( k shell). Based on this output, I need to call another perl script, with input arguments as the result set of the procedure execution. I need to keep looping through and call the perl script, ... (2 Replies)
Discussion started by: rajpreetsidhu
2 Replies

9. Shell Programming and Scripting

Capturing shell script command output

I am trying to check to see if a file exists on a ftp server, well, I know that cant be done, atleast directly, So I came up with this small script ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD cd public_html/crap dir $FILE quit END_SCRIPT Where the $ variable... (2 Replies)
Discussion started by: designflaw
2 Replies

10. Shell Programming and Scripting

capturing output in script

I have the following line in my script: $sftpcmd $rmthost <<COMMANDS>> $sftplog 2>&1 For some reason this is not capturing the errors from sftp, they go to the file attached to the cron entry ie mm hh dd MM * /myscript > cron.out any idea why? digital unix 4.0d (6 Replies)
Discussion started by: MizzGail
6 Replies
Login or Register to Ask a Question