highlight text of a file from specified line number
Hi all:
I am going through scripting and want a little help. There is a file with say about 50 lines and I want to hightlight/change the text color where the word is "leasing" starting from 25th line to end. How to do that? -t@nveer |
what kind of file are you even talking about?
|
simple text file.
|
Simple text files don't have colour information in them. You could insert ANSI colour codes in the file, but most text editors don't support them (although printing it on a terminal emulator using cat might look OK.)
|
Yes, I want to print from 25th line with the highlighted text in the terminal. I have found that highlighting can be done using 'grep' but not sure how to do that. Any examples ?
Thanks in advance. -t@nveer |
Code:
export GREP_OPTIONS=--colour=auto |
To elaborate on nx5000's comment in post #5, GNU's implementation of grep can change the colour of matched patterns in the output. However it does not do this by default.
You can tell grep to colour-highlight matched patterns with the --colour option. --colour (or --color if you prefer) takes an optional parameter which tells grep under what conditions to colourise the output. The options are "never", "always" and "auto". If you don't want to use this option on the command line, you can set the GREP_COLOR environment variable, as nx5000 suggested. The auto mode will colourise output if it goes to a terminal, but not if the output it piped to another program. This is because most programs will fail to process colour information properly. You can force piped output using the always option. If you like to view the output with less, you have to tell less to process control codes in raw mode. Here's how to see pattern matches with colour and see the output with less. Code:
grep --colour=always pattern myfile |less -r |
Hi, tanveer.
A quick hack: Code:
#!/usr/bin/perl Code:
#color-on forest Code:
./p4 data-file Now is the time for all good people to come to the aid of their country. |
You could use sed and a couple of shell variables. Saves writing a whole program.
For example to colour the text in lines 10 to 15 brown (yes, I'm an Ubuntu user - long live the colour brown! All hail to the colour brown!), you might do something like this: Code:
col_brown=$(echo -e "\033[40m\033[33m") |
Thank you all for your help.
matthewg42, it worked like a charm. But the thing I wanted was to highlight only specific word if matches between a line range. Your solution highlights everything in a line range. I better go with grep option as you and nx5000 said. I am using it like this:- Code:
[tanveer@server Test]$ export GREP_OPTIONS=--colour=auto Code:
sed '28,31 p' a | grep --colour=always 'base' | less -r| uniq -t@nveer |
All times are GMT -5. The time now is 11:13 AM. |