16

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
  • 5
    you need to escape the special characters by a backslash character. Like: /ERROR \[section_NAME\] Commented Apr 6, 2016 at 13:36
  • Have you tried to use /ERROR \[section_NAME\] Commented Apr 6, 2016 at 13:36
  • No, that did not work. I tried that now. Commented Apr 6, 2016 at 13:59
  • If the logfile has coloured text in it, you will have issues matching any of that. Commented Apr 17, 2018 at 17:21

2 Answers 2

23

The manual does mention you can switch off regular expression search for the whole search string by pressing ^R (CTRL+R) after you press /.

1
  • 2
    @ninjaloop Accept this headache-preventing answer! Commented Aug 16, 2019 at 3:25
-1

If you have GNU regex on your system (re_compile_pattern is found in C lib), then the escape character is the usual backslash (\). However, there are many different regex engines that less may be using, depends on how it was compiled: https://github.com/gwsw/less/blob/master/pattern.c#L25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.