Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Linux Best Way to Highlight text within Log File

In Linux was is the best command to use to search for and highlight text when in a log file that matches or starts with a specific sequence. For example should I use more, less , VI...etc..
0
compdigit44
Asked:
compdigit44
  • 6
  • 3
  • 3
  • +4
3 Solutions
 
Seth SimmonsSr. Systems AdministratorCommented:
depends on what distribution and version
newer releases will have highlighting when searching for strings in more, less, vi, emacs, grep
0
 
compdigit44Author Commented:
Thanks...

I found how to search for text using vi but was not successful using more or less. What is the proper syntax of each on Redhat / Fedora release
0
 
Seth SimmonsSr. Systems AdministratorCommented:
commands are the same on both distributions

you can either use it directly or pipe text

more foo.txt
cat foo.txt | more


less foo.txt
cat foo.txt | less


either way you can press forward slash (/) while in it then type your search pattern
0
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

 
compdigit44Author Commented:
thanks...

for / startng the search ...

Why would you use cat over more or less...
0
 
compdigit44Author Commented:
When I type in more filename it open but when I type "/" it states: Enter:next line Space: next page Q: quit R:show the rest....
0
 
Nick UpsonPrincipal Operations EngineerCommented:
more myfile
/ mystring

you could use vim, which has highlighting

or use grep, which will search the entire file non-interactive
0
 
savoneCommented:
Grep is most commonly used to search for strings inside of a text file.  It will (sometimes depending on version) highlight the text, but it will also only show the lines with the search string in them

example:

grep string filename.log
0
 
compdigit44Author Commented:
Thank you very much... I am trying to understand what I got this message thought when trying to search for text using the more command... Enter:next line Space: next page Q: quit R:show the rest....

I did select / < stringName>
0
 
Nick UpsonPrincipal Operations EngineerCommented:
th emore command has shown a page (or believes it has) and is prompting you to see if you want the next line or page or the rest ...
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
In less, you can search for a text in forward direction using /<string> and <enter>.
If you want to search in backward direction it is ?<string> and <enter>. If a matching string is found, it is highlighted on screen. To repeat the search in the same direction press n. You can press g to go to the first line and G to go to the last line.

Can you show a sample of the sequence you're looking for and a sample file maybe?
0
 
serialbandCommented:
I would also suggest installing mtail to syntax highlight your log files.  http://freecode.com/projects/mtail
0
 
Duncan RoeSoftware DeveloperCommented:
less has far more facilities than more. In less, searches are regular expressions, for which see man grep.
To see what version of less you have, enter less --version. This can be more important to know than the distribution.
I have less 451 (POSIX regular expressions) and as far as I can tell this implements everything in the grep man page. In particular, you can search for a string as a word by enclosing it in escaped angle brackets e.g.
/\<PAGE\>

Open in new window


in my .bashrc highlights PAGE but not PAGER.
Note there must not be a space between the forward-slash and the search string.
Please post sample strings you are trying to find if you need more to go on.
0
 
Duncan RoeSoftware DeveloperCommented:
Also in less you can use N to search in the opposite direction from n
If you press h, you get help on this and all the other available controls
0
 
compdigit44Author Commented:
Thanks everyone... I and just tring to understand things better..

On a sample log file I type in: more syslog.log and can view the file page by page. When I type / <string> it does not search for text...

When I use less and the / command it does work..

I am also trying to understand Seth Simmons response post number 40532700
0
 
savoneCommented:
Using the more command is a bad way to search for text.  Using the +/"string" will search, but it will show the complete file after finding the first match to the string.

Here is an example:

more +/"string" filename.txt

The grep command is more suited for searching strings and displaying lines with those strings.

As far as Seth's comments he was just showing the basic usage of more and less commands.
0
 
compdigit44Author Commented:
thanks...

It sounds like if I want to search for text in a file grep and less are best choices
0
 
savoneCommented:
I would say grep would be your best option.
0
 
Nick UpsonPrincipal Operations EngineerCommented:
less is interactive
grep is not

you may want to do "grep string filename | less"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 3
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now