• Display lines without certain text

    From Cecil Westerhof@Cecil@decebal.nl to gnu.emacs.help on Wed May 18 13:39:26 2022
    From Newsgroup: gnu.emacs.help

    I have a log file in which most lines are not interesting most of the
    time. These line all contain a certain string. Is there a way to
    display only those lines that do not contain a certain string?
    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From USEnet@jvromans@squirrel.nl to gnu.emacs.help on Wed May 18 14:05:07 2022
    From Newsgroup: gnu.emacs.help

    On Wed, 18 May 2022 13:39:26 +0200, Cecil Westerhof <Cecil@decebal.nl>
    wrote:

    I have a log file in which most lines are not interesting most of the
    time. These line all contain a certain string. Is there a way to
    display only those lines that do not contain a certain string?

    I use M-x delete-matching-lines but this changes the buffer, so be careful
    not to accidentally save...

    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Cecil Westerhof@Cecil@decebal.nl to gnu.emacs.help on Wed May 18 15:42:22 2022
    From Newsgroup: gnu.emacs.help

    USEnet <jvromans@squirrel.nl> writes:

    On Wed, 18 May 2022 13:39:26 +0200, Cecil Westerhof <Cecil@decebal.nl>
    wrote:

    I have a log file in which most lines are not interesting most of the
    time. These line all contain a certain string. Is there a way to
    display only those lines that do not contain a certain string?

    I use M-x delete-matching-lines but this changes the buffer, so be careful not to accidentally save...

    The problem with that is that when it is a log file that is still
    written to, that does not work anymore.

    But I could write a little script that copies to a temp file and then
    uses delete-matching-lines.
    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Ben@ben.usenet@bsb.me.uk to gnu.emacs.help on Wed May 18 15:47:38 2022
    From Newsgroup: gnu.emacs.help

    Cecil Westerhof <Cecil@decebal.nl> writes:

    I have a log file in which most lines are not interesting most of the
    time. These line all contain a certain string. Is there a way to
    display only those lines that do not contain a certain string?

    I'd use grep (the Emacs command that runs grep) but using the -v
    argument that show only non-matching lines.

    But out of Emacs, I'd use

    $ tail -f logfile | grep -v boring-string

    since that "follows" the file, showing new lines as they appear. (When
    I say "out of Emacs" you can, of course, run this in a Emacs shell
    window.)
    --
    Ben.
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From USEnet@jvromans@squirrel.nl to gnu.emacs.help on Wed May 18 17:11:19 2022
    From Newsgroup: gnu.emacs.help

    If you're going to copy the file you might as well grep out the boring
    string lines, as per Ben's recipe.

    -- Johan

    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Cecil Westerhof@Cecil@decebal.nl to gnu.emacs.help on Wed May 18 17:26:37 2022
    From Newsgroup: gnu.emacs.help

    Ben <ben.usenet@bsb.me.uk> writes:

    Cecil Westerhof <Cecil@decebal.nl> writes:

    I have a log file in which most lines are not interesting most of the
    time. These line all contain a certain string. Is there a way to
    display only those lines that do not contain a certain string?

    I'd use grep (the Emacs command that runs grep) but using the -v
    argument that show only non-matching lines.

    But out of Emacs, I'd use

    $ tail -f logfile | grep -v boring-string

    since that "follows" the file, showing new lines as they appear. (When
    I say "out of Emacs" you can, of course, run this in a Emacs shell
    window.)

    That would also be an option, but at the moment I chose the following
    function:
    (defun dcbl-reduced-logfile (filter-string)
    (interactive "s Filter String: ")
    (message filter-string)
    (let ((buffer-name (generate-new-buffer-name "reduced-logfile"))
    (text (buffer-substring (point-min) (point-max))))
    (switch-to-buffer buffer-name)
    (insert text)
    (beginning-of-buffer)
    (delete-matching-lines filter-string)
    ))

    Just whipped it up, so I am open for improvements.
    (Should probably put name of current buffer in the new buffer name.)
    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Cecil Westerhof@Cecil@decebal.nl to gnu.emacs.help on Wed May 18 19:32:54 2022
    From Newsgroup: gnu.emacs.help

    Cecil Westerhof <Cecil@decebal.nl> writes:

    Ben <ben.usenet@bsb.me.uk> writes:

    Cecil Westerhof <Cecil@decebal.nl> writes:

    I have a log file in which most lines are not interesting most of the
    time. These line all contain a certain string. Is there a way to
    display only those lines that do not contain a certain string?

    That would also be an option, but at the moment I chose the following function:
    (defun dcbl-reduced-logfile (filter-string)
    (interactive "s Filter String: ")
    (message filter-string)
    (let ((buffer-name (generate-new-buffer-name "reduced-logfile"))
    (text (buffer-substring (point-min) (point-max))))
    (switch-to-buffer buffer-name)
    (insert text)
    (beginning-of-buffer)
    (delete-matching-lines filter-string)
    ))

    Just whipped it up, so I am open for improvements.
    (Should probably put name of current buffer in the new buffer name.)

    A little bit better version:
    (defun dcbl-reduced-logfile (filter-string)
    (interactive "sFilter String: ")
    (message filter-string)
    (let ((buffer-name (generate-new-buffer-name
    (concat "reduced-logfile"
    "#"
    filter-string
    "#"
    (file-name-sans-extension (buffer-name)))))
    (text (buffer-substring (point-min) (point-max))))
    (switch-to-buffer buffer-name)
    (insert text)
    (beginning-of-buffer)
    (delete-matching-lines filter-string)
    ))

    My log files are often like 2022-05-18.log, so it is handy to have the
    filter string included.
    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof
    --- Synchronet 3.21d-Linux NewsLink 1.2