Perl searching and printing multiple target in the same line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Perl searching and printing multiple target in the same line
# 1  
Old 04-24-2009
Perl searching and printing multiple target in the same line

Hello,

I'm trying to create a program in perl called myfind.pl;

To use the program:
(at the command line)$ program.pl keyword filename
note: the keyword is any word or regular expression

and it should display the result just like when you 'cat' the file name but with the keyword in different color.

the problem is the program works fine if it's only one match in a line but it wouldn't get the 2nd match on the same line. I tried many logic and it didn't work out.

This is what i want it to look like

example: $ myfind.pl hot weather.txt
result: Yesterday was very hot and i had a cup of hot tea with a hot soup

my code right now looks like this and it works fine for getting a match per line but the rest of the match wouldn't be in color as the first match.



use strict;

unshift(@INC, "Xcolors.pm");
use Xcolors qw(:group_fg :group_bg :group_misc);

my $obj = "Xcolors"->new();

my $xrex = shift;
my $xfile = shift;

open(FH,"$xfile")||die("cannot open '$xfile': $!\n");
while(my $line=<FH>)
{
if($line =~ $xrex)
{

$_ = $line;
/$xrex/;

print "$`";
$obj->fred();
print "$&";
$obj->reset();
print "$'";
}

else
{
$obj->reset();
print "$line";
}
}
close(FH)||die("cannot close '$xfile': $!\n");


Any suggestion is really appreciated,
Thank you.

Last edited by Horizon666; 04-24-2009 at 05:58 PM..
# 2  
Old 04-24-2009
to match globally, use the "g" modifier. check perldoc perlretut or perldoc perlre
# 3  
Old 04-25-2009
i actually use grep in this program because i need to display the output as it is which mean i need to print the string before the target and after the target and then re-searching the string after the target again for another match and repeat the process through the entire line

s command actually doesn't work for me in this case because i need to call the color method to print in color. If i use it in the substitute part, it will only print 1 for me.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing multiple lines on the same line between specific text

This is an extract from a large file. The lines that start with fc are ports on a fabric switch. In between each fc port there is information about the port. fc2/12 is up Port description is SEIEDISCOVER-3 Speed is 4 Gbps fc2/13 is down (Administratively down) fc2/14 is up Port... (1 Reply)
Discussion started by: kieranfoley
1 Replies

2. Shell Programming and Scripting

Searching and printing the only pattern using awk,sed or perl

Hi All, i have an output of command vmstat as below : $ vmstat System configuration: lcpu=4 mem=5376MB ent=1.00 kthr memory page faults cpu ----- ----------- ------------------------ ------------ ----------------------- r b avm fre re pi... (10 Replies)
Discussion started by: omkar.jadhav
10 Replies

3. UNIX for Dummies Questions & Answers

Grep in Perl - Searching through multiple files

I'm attempting to use grep in Perl with very little success. What I would like to do in Perl is get the output of the following grep code: grep -l 'pattern' * This gives me a list of all the files in a directory that contain the pattern that was searched. My attempts to do this in Perl... (4 Replies)
Discussion started by: WongSifu
4 Replies

4. Shell Programming and Scripting

Need help in Perl Script for printing next line

I got multiple of documents in which i have to extract a line coming after a ID..The scenario is Customer ID: none VT : 002/89 Customer ID: Yes VT: 001/89 Customer ID: none VT: 006/85 Customer ID: Yes VT: 003/56 I have to extract the id which is coming after YES..The output... (7 Replies)
Discussion started by: rajkrishna89
7 Replies

5. Shell Programming and Scripting

How to bin/find w/ -follow without searching both link and target

I am interested in searching links to files not found within a directory, so I use the -follow option. However, the dir may contain links to files that are also found within the dir. That means if I bin/find a bunch of files then search their contents using grep, I get redundant information. An... (1 Reply)
Discussion started by: stevensw
1 Replies

6. UNIX for Dummies Questions & Answers

Searching for multiple words on a line in any order issue

Hi again I have figured out how to be able to sort through lines in a file with multiple words in any order and display them using this command: cat file | grep -i $OPTION1 | grep -i $OPTION2 | grep -i $OPTION3 OPTION1 is 2008, OPTION2 is Mar, OPTION 3 is Tue Result: Tue Mar 25... (4 Replies)
Discussion started by: semaj
4 Replies

7. Shell Programming and Scripting

searching multiple patterns in perl

Hi, I have code like: Output it is comming as: Rels: WM2 Rels: WG2 Rels: 5 - pre/prods.pl Rels: 6 Rels: 7 Rels: 8 Rels: 10 Rels: Int But i want only "Rels: 5" pattern Just above "- pre/prods.pl". By... (7 Replies)
Discussion started by: Anjan1
7 Replies

8. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

9. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

10. Shell Programming and Scripting

printing an empty line in a file (perl)

I know this must be really easy, but i can't get it to work I've got a perl script, with a file. I want to print an empty line, and the following doesn't seem to work: print nameoffile "\n" thanks for your help!! (3 Replies)
Discussion started by: kfad
3 Replies
Login or Register to Ask a Question