LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 12-04-2006, 01:50 PM   #1
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 512

Rep: Reputation: 37
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
 
Old 12-04-2006, 01:58 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
what kind of file are you even talking about?
 
Old 12-04-2006, 02:09 PM   #3
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 512

Original Poster
Rep: Reputation: 37
simple text file.
 
Old 12-04-2006, 02:26 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.)
 
Old 12-04-2006, 10:43 PM   #5
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 512

Original Poster
Rep: Reputation: 37
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
 
Old 12-05-2006, 07:01 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 53
Code:
export GREP_OPTIONS=--colour=auto
 
Old 12-05-2006, 09:10 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
 
Old 12-06-2006, 01:26 PM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
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.
 
Old 12-06-2006, 01:51 PM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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/;"
 
Old 12-06-2006, 11:27 PM   #10
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 512

Original Poster
Rep: Reputation: 37
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

Last edited by tanveer; 12-06-2006 at 11:41 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash read a given line number from text bendeco13 Programming 7 08-31-2012 04:49 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 12:55 PM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 03:02 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 05:32 PM


All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration