LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-05-2010, 11:36 AM   #1
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Rep: Reputation: 0
Need a perl script


Hello,

I would like to know a how to write Perl script to catch all the content in between BEGIN: and END from input file could any body help?

BEGIN:-----------------------------------------------
test 1
test 2 %%%%%%%%%% TEST TEST TEST
test 3
END:

Thanks
 
Old 08-05-2010, 12:00 PM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
If it's all right it's not Perl, here's a GNU sed script I happened to write this morning for exactly this thing (yeah, quick and ugly)

Code:
sed -n '/BEGIN/,/END/{/BEGIN\|END/!p}' inputfile
It types everything between BEGIN and END, excluding those. Fine-tune if you want.

Edit: all right, seems this page has the Perl equivalent. Appears to do about the same as the GNU sed script I wrote, i.e. find the starting point, then print all lines that don't match either starting or ending mark up to the ending mark.

Last edited by b0uncer; 08-05-2010 at 12:06 PM.
 
Old 08-05-2010, 12:06 PM   #3
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Code:
#!/usr/bin/perl -w
$file=open(f, "test.txt");
$str="";
while(<f>) {$str.=$_;}
close(f);
$str =~ s/BEGIN:(.*)END:/$1/egs;
print $str
 
Old 08-05-2010, 12:09 PM   #4
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by troop View Post
Code:
#!/usr/bin/perl -w
$file=open(f, "test.txt");
$str="";
while(<f>) {$str.=$_;}
close(f);
$str =~ s/BEGIN:(.*)END:/$1/egs;
print $str
Doesn't that include the dashes from the line containing the starting mark? I thought they shouldn't be included...
 
Old 08-05-2010, 11:18 PM   #5
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Quote:
Originally Posted by b0uncer View Post
Doesn't that include the dashes from the line containing the starting mark? I thought they shouldn't be included...
Code:
- $str =~ s/BEGIN:(.*)END:/$1/egs;
+ $str =~ s/BEGIN:[^\n]*\n(.*)\nEND:/$1/egs;
 
Old 08-06-2010, 07:40 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Do you want the BEGIN and END to be let through or not?

If not, then this:

Perl:

Code:
my $inside = 0
while <STDIN> {
    $inside = 0 if (/^END:/);
    print if ($inside);
    $inside = 1 if (/^BEGIN:/);
}
Or, if you DO want it to print the BEGIN and END lines:

Code:
my $inside = 0
while <STDIN> {
    $inside = 0 if (/^BEGIN:/);
    print if ($inside);
    $inside = 1 if (/^END:/);
}
 
Old 08-06-2010, 11:40 PM   #7
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Original Poster
Rep: Reputation: 0
Thank you & one more script example

I got it thank you for all now I have only one more as a learning part

I have below details in a file and now I need to get the name and address of a particular person based on the id, I know we need to key pair value.

id name adress
1 chris san jose
2 pinky manglore
3 arp puttur

Thanks again
 
Old 08-07-2010, 05:05 AM   #8
tealio
LQ Newbie
 
Registered: Aug 2010
Location: Atlanta, GA
Distribution: Ubuntu 10.04
Posts: 23

Rep: Reputation: 18
I *think* the join command will do that.
 
Old 08-09-2010, 12:14 AM   #9
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by sharp859 View Post
I have below details in a file and now I need to get the name and address of a particular person based on the id, I know we need to key pair value.

id name adress
1 chris san jose
2 pinky manglore
3 arp puttur

Thanks again
If the data is separated by tabs (\t), then something like this might do

Code:
awk -F"\t" '/^2/{print "name:", $2 "\naddress:", $3}' datafile
See the output and tune to your liking, if it fits.

To use it in a script, you'd replace the /^2/ with /^id-you-want/ using a variable, perhaps.

Last edited by b0uncer; 08-09-2010 at 12:18 AM. Reason: space/off
 
Old 08-09-2010, 03:51 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
If you're reading a flat file and know the id at input, then you'd read the file rec by rec and use the split(0 fn on spaces eg
Code:
($name, @addr) = split(/\s+/, $rec);
 
  


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
problem with perl modules declaration and perl/cgi script shifter Programming 9 02-24-2010 09:09 AM
call perl script through another perl script with arguments nanda22 Linux - Newbie 21 07-21-2009 12:18 AM
NEED HELP IN comment lines PERL Perl script adam_blackice Programming 17 11-07-2007 08:01 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM


All times are GMT -5. The time now is 06:41 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