LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-27-2009, 02:32 PM   #1
realnoname
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Rep: Reputation: 0
grep variables in perl


i have been using this grep command in my perl script to take out all the stuff i dont need but the way it works now is it gets the text from a file and then saves it to another file but what i would like to do is just use variables only and not files but i cant get anything to work, any ideas?

Code:
`grep -E -v '^.*?2/24.*?\$' /tmp/temp1 >> temp2`;
 
Old 02-27-2009, 02:35 PM   #2
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
Why have you been opening a shell to run grep? Perl has a built-in grep function. Take a look at the perldoc for more information.
 
Old 02-27-2009, 02:46 PM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by Poetics View Post
Why have you been opening a shell to run grep? Perl has a built-in grep function. Take a look at the perldoc for more information.
Perl's grep isn't equivalent to the shell's grep. They share a name, but they don't do quite the same things. The grep function in Perl filters lists based on tests of some kind. Example:
Code:
my @numbers = qw/1 2 3 4 5 6/;
@numbers = grep { $_ % 2 == 0 } @numbers;
print @numbers;
The test inside the grep block tests for even numbers and filters the list accordingly. So this code produces:
Code:
hektor ~ $ ./grepper 
2 4 6
That said, there's no reason to use the shell utility grep inside a Perl script, since Perl's regular expressions are second to none.

@ realnoname: Here's how to do what I think you want to do:
Code:
open my $input, '<', '/tmp/text1'
  or die "Can't open '/tmp/text1' for reading: $!";
open my $output, '>>', 'temp2'
  or die "Can't open 'temp2' for writing: $!";

while (<$input>) {
  # Some code here to get the lines you want or edit the lines
  # in the file
  print $output $_;
}

Last edited by Telemachos; 02-27-2009 at 02:59 PM.
 
Old 02-27-2009, 02:52 PM   #4
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
Very true, but if he was grabbing lines from a file, using perl, he's likely putting them into an array and grabbing from there. With everything in the perldoc (and the regexps you've alluded to) he should be fine
 
Old 02-27-2009, 02:58 PM   #5
realnoname
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
i already have the text that i need in a variable (it is never in a file anymore)

am looking into using perls regex but if you guys have any pointers for me that would be great
 
Old 02-27-2009, 03:12 PM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by realnoname View Post
i already have the text that i need in a variable (it is never in a file anymore)

am looking into using perls regex but if you guys have any pointers for me that would be great
I have no idea what you're saying then. Originally, you said you get the text from a file (using grep from the shell). Now you say it's in a variable already. Ok, great. Does that mean you solved your original problem?

Let me put it another way: what are you asking for pointers about?
 
Old 02-27-2009, 03:33 PM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by realnoname View Post
i already have the text that i need in a variable (it is never in a file anymore)

am looking into using perls regex but if you guys have any pointers for me that would be great
Be a lot easier if you just told us what you wanted to do exactly and gave us an example of the data.
 
Old 02-27-2009, 07:46 PM   #8
realnoname
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
alright, i just need some help getting started

the first thing i need to do is remove any line that doesn't start with "sw-port"

the text is all with in one variable, say something like

$output = "getting hosts

Location: Room 15
sw-port - 2/24 - disabled
sw-port - 2/1 - connected
sw-port - 2/7 - connected
sw-port - 2/24 - disabled
"
 
Old 02-27-2009, 09:49 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by realnoname View Post
i already have the text that i need in a variable (it is never in a file anymore)

am looking into using perls regex but if you guys have any pointers for me that would be great
perldoc perlre
perldoc perlretut
 
Old 02-27-2009, 10:09 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by realnoname View Post
alright, i just need some help getting started

the first thing i need to do is remove any line that doesn't start with "sw-port"

the text is all with in one variable, say something like

$output = "getting hosts

Location: Room 15
sw-port - 2/24 - disabled
sw-port - 2/1 - connected
sw-port - 2/7 - connected
sw-port - 2/24 - disabled
"
in the first place, how are you getting all those strings into your variable? you hardcoded inside your script? It must come from some output of a command right?
 
Old 02-28-2009, 05:59 AM   #11
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
It is much easier to treat separate items as "lines" if you have them in an array rather than a single scalar variable. Here's what I might do.
Code:
#!/usr/bin/env perl
use strict;
use warnings;

my $output = "getting hosts

Location: Room 15
sw-port - 2/24 - disabled
sw-port - 2/1 - connected
sw-port - 2/7 - connected
sw-port - 2/24 - disabled
";

my @outputs = grep { /^sw-port/ } split /\n/, $output;

print "$_\n" foreach @outputs;
Which gives:
Code:
telemachus ~/practice $ ./grepper 
sw-port - 2/24 - disabled
sw-port - 2/1 - connected
sw-port - 2/7 - connected
sw-port - 2/24 - disabled
The print statement at the end is just an example. I obviously don't know what you really want to do with those lines.

Edit: by the way, it would be better overall not to put the whole file into a single scalar variable. Instead, when you read it in, put it into an array (or maybe a hash, depending on what you ultimately want to do with it).

Last edited by Telemachos; 02-28-2009 at 06:24 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep the variables - Script rajaniyer123 Solaris / OpenSolaris 3 08-09-2008 04:48 PM
Can you use grep / awk on variables instead of files? zest n zeal Linux - Newbie 2 02-11-2008 12:37 PM
grep and assign it's output to variables inside script itself problem xxx_anuj_xxx Programming 3 09-22-2007 11:24 PM
Bash: How can i use variables for grep regular expresions ? landuchi Linux - General 4 01-21-2006 11:57 PM
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM

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

All times are GMT -5. The time now is 04:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration