LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-19-2009, 08:24 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
selecting few lines in perl


hi all....

In a file following are the contents........

(
etc,
ls,
/sbin/usr
.....
...
...
)
command
dksal
.......

I know how to match a pattern in a single line and get it in $1 but
here i want to select the lines between two braces ( and ) how can i accomplish this in perl

can anyone please help me.......
Thanks in advance......
 
Old 06-19-2009, 02:21 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
owzthat?

Code:
#!/usr/bin/perl

local $/ = undef;  # slurp mode

$slurp = <>;    # read whole file
($lines) = ($slurp =~ /\((.*)\)/s);    # 
print $lines;
 
Old 06-19-2009, 06:21 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vinaytp View Post
but
here i want to select the lines between two braces ( and ) how can i accomplish this in perl
when the line match ")", remove flag
when the line match "(" , set a flag
if flag is set, print line
 
Old 06-19-2009, 07:08 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
while (<>) {
    print if /\(/ .. /\)/;
}

Last edited by Tinkster; 06-19-2009 at 07:09 PM. Reason: shhhhorter ;D
 
Old 06-20-2009, 04:31 AM   #5
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by bigearsbilly View Post
owzthat?

Code:
#!/usr/bin/perl

local $/ = undef;  # slurp mode

$slurp = <>;    # read whole file
($lines) = ($slurp =~ /\((.*)\)/s);    # 
print $lines;
helped me a lot really great .......thanks a lot to everyone.......
 
  


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
Perl vs. bash code - number of lines noir911 Programming 11 10-07-2011 12:35 PM
NEED HELP IN comment lines PERL Perl script adam_blackice Programming 17 11-07-2007 08:01 AM
Perl: testing for blank lines Garda Programming 4 11-16-2006 07:39 PM
Perl and drawing lines Veteq Programming 2 11-27-2004 07:16 AM
Searching for 2 empty lines by RegEx in perl mosh Programming 8 09-08-2004 02:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:25 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
Open Source Consulting | Domain Registration