From Newsgroup: gnu.emacs.help
Cecil Westerhof <
Cecil@decebal.nl> writes:
I have a script in which I have the following function:
function doOutput {
echo -ne "$(date +"${pre}%T ${1}")" | tee --append "${_logfile}"
}
My script generates messages with:
doOutput "${1}"
pre='\n' # Next output should be on a new line
But I also have notifications which should be overwritten by the next
message and for this I use:
doOutput "${1}\r"
pre='' # Next output should be on the same line
Works OK. But in the logfile I get:
16:15:00 Nothing has changed^M16:30:00 Nothing has changed^M16:45:00 Nothing has changed^M
Is there a way to let Emacs display this like:
16:15:00 Nothing has changed^M
16:30:00 Nothing has changed^M
16:45:00 Nothing has changed^M
I do not mind if it will be like:
16:15:00 Nothing has changed
16:30:00 Nothing has changed
16:45:00 Nothing has changed
1. LetrCOs assume that your scriptrCOs output is sent to a file
named "datelog.txt". Then running rCycat -erCO at the shell
prompt produces the following output:
$ cat -e datelog.txt
16:15:00 Nothing has changed^M16:30:00 Nothing has changed^M16:45:00 Nothing has changed^M
2. And the stream editor, "/bin/sed", can produce the desired
output with:
$ cat -e datelog.txt | sed -e "s/\^M/\n\n/g"
16:15:00 Nothing has changed
16:30:00 Nothing has changed
16:45:00 Nothing has changed
$
3. This output can be directed to a new file, if desired:
$ cat -e datelog.txt | sed -e "s/\^M/\n\n/g" > newlog.txt
4. Alternatively, this can be done in Emacs.
4a. Read datelog.txt into an Emacs buffer and interactively
call the built-in Emacs function for regular-expression
search-and-replace, rCyreplace-regexprCO:
M-x replace-regexp
When prompted for Replace regexp, type: \ C-q C-m <Enter>
When prompted for Replace regexp \^M with, type: C-q C-j <Enter>
(C-<key> means press and hold the Ctrl key while pressing
the other key. So, C-q means Ctrl-q.)
Emacs should then replace all instances of ^M with a newline
character and give the same results as rCysedrCO did, above.
The buffer would then need to be saved to make the changes
permanent in the file.
4b. Or, define a function (in your Emacs initialization file)
that calls the built-in Emacs function rCyreplace-regexprCO,
specifying the searched-for and replacement expressions.
You could then call the function whenever you wanted it to
be applied to a file that you had opened in Emacs.
4c. Or, you could add your new functionrCOs name as a hook to
one of the hook variables in Emacs (likely
rCytext-mode-hookrCO), which could be set in your Emacs
initialization file, so that the function was automatically
run whenever you opened your log file. This would likely
complicate your function definition because you would likely
want the search-and-replace to be run only on your date-log
files, not on all text files. So conditional code would
need to be included in your function definition to check,
say, the filerCOs name, before the search-and-replace call.
--
--- Synchronet 3.21d-Linux NewsLink 1.2