CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Search and grep

 



Prka
New User

Dec 22, 2015, 10:22 AM

Post #1 of 5 (2252 views)
Search and grep Can't Post

Hello All,

I am started learning perl. this is the first time I am started coding for my project, To print the description of the element from a log book for my(class) reference.

I have learned to open a file and read them, Its difficult for me to grep from ABT**** and stop before USAGE... Its more than 1000 elements in the log. It will be much relief for me if you help me to code to get the output.

Details

Element name : Iron
Required purpose : Industrial xxxxx, xxxxxx and home use
ABT6543 : Iron reacts with oxygen in the air to form various oxide and hydroxide compounds; the most common are iron(II,III) oxide (Fe3O4), and iron(III) oxide (Fe2O3). Iron(II) oxide also exists, though it is unstable at room temperature.
USAGE: xxxxx xxxxx

Element name : Lithiyam
Required purpose : xxxx
ABT6543:xxxxx xxxxxx xxxxxx xxxx
USAGE: xxxxx xxxxx


FishMonger
Veteran / Moderator

Dec 22, 2015, 11:00 AM

Post #2 of 5 (2228 views)
Re: [Prka] Search and grep [In reply to] Can't Post

Please post your code along with any/all errors it produces.

If the "elements" are separated by a blank line, then you could loop over the file in paragraph mode and process each element block one at a time. To set it to paragraph mode, you assign the "" empty string to the $/ (Input Record Separator) variable.

Code
$/ = "";



Laurent_R
Veteran / Moderator

Dec 22, 2015, 1:40 PM

Post #3 of 5 (2219 views)
Re: [Prka] Search and grep [In reply to] Can't Post

If your relevant input (about iron) has just those four lines and you need to print just the line starting with ABT, then it could be as simple as this:


Code
while (my $line = <$INFILE>) | 
print $line if $line =~ /^ABT/;
}

(The code could be more concise, but I won't go into that for a beginner).
If what you need is more complicated, then please explain.


Prka
New User

Dec 22, 2015, 8:08 PM

Post #4 of 5 (2204 views)
Re: [Laurent_R] Search and grep [In reply to] Can't Post

I can print the single line with starting with ABT... The problem for me is the line starting with ABT have more than one line and vary for each element. So I need to start from "ABT" and stop in "Usage".
I don't know how to use the loop for this situation


Chris Charley
User

Dec 22, 2015, 8:33 PM

Post #5 of 5 (2199 views)
Re: [Prka] Search and grep [In reply to] Can't Post

This program uses the range operator, (the 2 dots .. between the regular expressions)


Code
#!/usr/bin/perl 
use strict;
use warnings;

while (<DATA>) {
if (/^ABT/ .. /^USAGE/) {
print unless /^USAGE/;
}
}

__DATA__
Element name : Iron
Required purpose : Industrial xxxxx, xxxxxx and home use
ABT6543 : Iron reacts with oxygen in the air to form various oxide
and hydroxide compounds; the most common are iron(II,III) oxide (Fe3O4),
and iron(III) oxide (Fe2O3). Iron(II) oxide also exists,
though it is unstable at room temperature.
USAGE: xxxxx xxxxx

Element name : Lithiyam
Required purpose : xxxx
ABT6543:xxxxx xxxxxx xxxxxx xxxx
USAGE: xxxxx xxxxx


Output is:

Code
ABT6543 : Iron reacts with oxygen in the air to form various oxide 
and hydroxide compounds; the most common are iron(II,III) oxide (Fe3O4)
and iron(III) oxide (Fe2O3). Iron(II) oxide also exists,
though it is unstable at room temperature.
ABT6543:xxxxx xxxxxx xxxxxx xxxx

The if statement becomes true when the match on the left succeeds, /^ABT/, and stays true until the match on the right, /^USAGE/ when it becomes false until the next ABT line occurs.

(It will allow the line USAGE at the end so check for that in the print).

Update: Just realized this might be made clearer by explicitly opening a file with your sample data, 'elements.txt' like you probably will. Also, read each line of the file into the '$line' variable rather than using the default '$_' variable as above. The program now is (and produces the same output):


Code
#!/usr/bin/perl  
use strict;
use warnings;

my $file = 'elements.txt';
open my $fh, '<', $file or die "Unable to open $file: $!";

while (my $line = <$fh>) {
if ($line =~ /^ABT/ .. $line =~ /^USAGE/) {
print $line unless $line =~ /^USAGE/;
}
}
close $fh or die $!;



(This post was edited by Chris Charley on Dec 22, 2015, 9:03 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives