LinuxQuestions.org
Help answer threads with 0 replies.
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: 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, 02:21 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,973
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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: 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 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


All times are GMT -5. The time now is 03:05 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration