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)
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?
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.
Format the date to get the date-time stamp required
Now echo this to a file where you want to log this.
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.
Format the date to get the date-time stamp required
Now echo this to a file where you want to log this.
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.
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.
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!!
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
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)
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)
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)
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)
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)
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)
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)
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)