LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2004, 11:33 AM   #1
bishal
LQ Newbie
 
Registered: Feb 2004
Location: Nepal
Posts: 25

Rep: Reputation: 15
howto grep in perl


Hello all

I want to grep the only the selected text in perl. Can anyone tell me how to do that and exclude the remaining file in perl. Below is my output of my perl scripts and I want to grep "unlock" only and exclude the rest. How can i do it with split command?


[abc@root]/sbin/test.pl

# Power lock status: unlock
 
Old 07-13-2004, 02:52 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I assume you want to extract the word 'unlock' for Power lock status value

Code:
$str = "# Power lock status: unlock";
if( $str =~ /Power lock status/ )
{
    $str =~ s/^(.*)\: //;
    print "$str\n";
}

Last edited by keefaz; 07-13-2004 at 02:56 PM.
 
Old 07-14-2004, 01:46 AM   #3
bishal
LQ Newbie
 
Registered: Feb 2004
Location: Nepal
Posts: 25

Original Poster
Rep: Reputation: 15
thanks kefaz it worked out. Can you explain me that second and 4th line of string and what does that tilde do?
 
Old 07-14-2004, 07:12 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
if( $str =~ /Power lock status/ )
Here, the regular expression (between / and / ) is used to match something so the =~ action related to $str is to search the pattern between / and / in it.

$str =~ s/^(.*)\: //;
Here the regular expression syntax is s///, it is to do a search and replace like :
$str = "this milk shake is too hot";
$str =~ s/hot/cold/;

This will replace hot by cold in the $str string.
^ : search from beginning of string
(.*) : any character match
"\: " : match a ':' folowed by a space

So the sequence s/^(.*)\: //; will replace all characters followed by a ':' and a space by nothing (//), in other words, it will delete all characters followed by a ':' and a space ( the ':' and space inclued).

Last edited by keefaz; 07-14-2004 at 07:13 AM.
 
Old 07-15-2004, 12:37 AM   #5
bishal
LQ Newbie
 
Registered: Feb 2004
Location: Nepal
Posts: 25

Original Poster
Rep: Reputation: 15
Thank you keffaz
 
Old 07-18-2004, 06:32 AM   #6
bishal
LQ Newbie
 
Registered: Feb 2004
Location: Nepal
Posts: 25

Original Poster
Rep: Reputation: 15
ok keefaz I have another question. How to ignore the last line also in perl. Example:
$line= /# milk in liter : 10.12[ltr]/;

I want only "10.12"

How to do that?
 
Old 07-18-2004, 07:19 AM   #7
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Quote:
Originally posted by bishal
I have another question. How to ignore the last line also in perl. Example:
$line= /# milk in liter : 10.12[ltr]/;

I want only "10.12"

How to do that?
Code:
#!/usr/bin/perl -w

$str = '# milk in liter: 10.12[ltr]';

if ( $str =~ /^.*:\s([0-9\.]+).*$/ ) {
  print( "$1\n" );
}
"^" matches the beginning of the line.

".*" matches zero or more of any character ("." matches any character, and "*" tells how many of them to match.) In this case, it matches "# milk in liter"

":\s" matches the colon followed by the space.

"([0-9\.]+)" is the key to the whole thing. Anything that matches the expression between "(" and ")" can be used later via the "$1" variable. See how we use it in the print statement? If you have multiple sets of "(" and ")" then you access them later via "$1", "$2", etc. The "[" and "]" represent a "character class". It means to match any of the characters between the "[" and "]". In this case we match any number 0 through 9, or the dot. The dot has to be escaped with a "\" otherwise it's the same as "any character". The "+" afterword means match one or more of the characters in the preceding character class. So, "[0-9\.]+" matches 10.12, 0.5, 10, ..1, 1.2.3.4, etc. We ignore the rest of the line by matching zero or more of anything, again using ".*".

"$" matches the end of the line.

If you wanted to capture the units (i.e. ltr) then you could do:

Code:
#!/usr/bin/perl -w

$str = '# milk in liter: 10.12[ltr]';

if ( $str =~ /^.*:\s([0-9\.]+).*\[(.*)\].*$/ ) {
   print( "$1\t$2\n" );
}
We'll leave it as an exercise for the reader to figure out that regular expression.

Eric

Last edited by eric.r.turner; 07-18-2004 at 07:23 AM.
 
Old 07-19-2004, 01:32 AM   #8
bishal
LQ Newbie
 
Registered: Feb 2004
Location: Nepal
Posts: 25

Original Poster
Rep: Reputation: 15
thanks eric, it worked out.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to assign result of grep to var in PERL amytys Programming 1 09-23-2004 06:25 PM
grep issue of escaping backticks in perl rajatgarg Programming 2 05-27-2004 01:27 AM
Shells, editing and grep -- questions re: howto Jiawen Linux - Software 3 09-17-2003 03:00 PM
PERL: problem with grep ocularbob Programming 9 05-01-2003 05:36 PM
Need help with grep or perl FredrikN Programming 2 12-16-2001 11:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 AM.

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
Open Source Consulting | Domain Registration