3

How to search a log file in Linux and escape special characters like square brackets (i.e. [ and ])

Can someone point me in the right direction on this?

I less the log file as per below:

less system001A.LOG

Once I am in the log, I then press < (i.e. the smaller-than sign), and then I press forward-slash (i.e./) and type what I would like to search for:

/ERROR [section_NAME]

The problem is how to escape these brackets while I search? Because when I run this, it says no pattern/match is found, yet this in fact exists on the log.

  • 4
    you need to escape the special characters by a backslash character. Like: /ERROR \[section_NAME\] – MelBurslan Apr 6 '16 at 13:36
  • Have you tried to use /ERROR \[section_NAME\] – Valentin Bajrami Apr 6 '16 at 13:36
  • No, that did not work. I tried that now. – NinjaLoop Apr 6 '16 at 13:59
  • If the logfile has coloured text in it, you will have issues matching any of that. – Kusalananda Apr 17 '18 at 17:21
2

I just spent the last 10 minutes trying to figure this out, and I can't find any reference to an escape character. However, the help page does mention you can switch off regexes for the whole search string by pressing ^R (CTRL + r) after you press /

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.