LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   highlight text of a file from specified line number (https://www.linuxquestions.org/questions/linux-general-1/highlight-text-of-a-file-from-specified-line-number-507423/)

tanveer 12-04-2006 12:50 PM

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

acid_kewpie 12-04-2006 12:58 PM

what kind of file are you even talking about?

tanveer 12-04-2006 01:09 PM

simple text file.

matthewg42 12-04-2006 01:26 PM

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.)

tanveer 12-04-2006 09:43 PM

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

nx5000 12-05-2006 06:01 AM

Code:

export GREP_OPTIONS=--colour=auto

matthewg42 12-05-2006 08:10 AM

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

makyo 12-06-2006 12:26 PM

Hi, tanveer.

A quick hack:
Code:

#!/usr/bin/perl

# @(#) p4      Demonstrate color-code insertion.
# $Id$

# 2006.12.05 / makyo / Original.

use warnings;

# use strict;

$key_on  = "#color-on";
$key_off = "#color-off";
$escape  = "\033";
$restore = "restore";

%color = (
    restore  => "[00m",
    black    => "[00;30m",
    firebrick => "[00;31m",
    red      => "[01;31m",
    forest    => "[00;32m",
    green    => "[01;32m",
    brown    => "[00;33m",
    yellow    => "[01;33m",
    navy      => "[00;34m",
    blue      => "[01;34m",
    purple    => "[00;35m",
    magenta  => "[01;35m",
    cadet    => "[00;36m",
    cyan      => "[01;36m",
    gray      => "[00;37m",
    white    => "[01;37m"
);

while (<>) {
    chomp;
    if (/^$key_on/) {
        ($color) = (split)[1];
        if ( not exists $color{$color} ) {
            print STDERR " Color name :$color: does not exist, ignored.\n";
        }
        else {
            print "$escape$color{$color}";
        }
        next;
    }
    if (/^$key_off/) {
        print "$escape$color{$restore}";
        next;
    }
    print "$_\n";
}
print "$escape$color{$restore}\n";

exit(0);

Allows a file such as:
Code:

#color-on forest
Now is the time
#color-on firebrick
for all good people
#color-off
to come to the aid
#color-on purple
of their country.

to be processed with:
Code:

./p4 data-file
to get color codes inserted. This may or may not be able to be printed on your system, but it should show up in a terminal window (simulated below) ... cheers, makyo

Now is the time
for all good people
to come to the aid
of their country.

matthewg42 12-06-2006 12:51 PM

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")
col_orig=$(echo -e "\033[0m")
sed "10 s/^/$col_brown/; 15 s/\$/$col_orig/;"


tanveer 12-06-2006 10:27 PM

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

[tanveer@server Test]$ grep --colour=always 'device' /home/tanveer/myfile | less -r

But how to mention the line range, using sed? like :-
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.