LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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, 09:24 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

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, 03:21 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,382

Rep: Reputation: 193Reputation: 193
owzthat?

Code:
#!/usr/bin/perl

local $/ = undef;  # slurp mode

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

Rep: Reputation: 243Reputation: 243Reputation: 243
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, 08:08 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,059
Blog Entries: 11

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

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

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


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


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