Perl print between 2 patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl print between 2 patterns
# 1  
Old 06-22-2011
Perl print between 2 patterns

I have been unable to find this anywhere; I have a multiline variable, and I want to print the text between two patterns in that variable. So the variable is

Code:
My
real
name
is
not
Deadman

And I need the output to be this, by printing between "real" and "not"
Code:
name
is

or including the two patterns; doesn't matter.

The only ways I've seen of doing this don't involve searching a variable, only hardcoded __DATA__ or files

EDIT: by the way, sorry if I didn't make it clear, I want it all in perl, it's a perl script, not using other languages.

Last edited by killer54291; 06-24-2011 at 10:33 AM..
# 2  
Old 06-23-2011
A string is a string, despite newlines, carriage returns, and sometimes nulls, never mind multicharacter like eucjis AND utf8.

I thought trimming a string in PER using regex was simple, let me google it.

Yep, like inline sed: The world is amazing: Perl: Trim a string

Then print the trimmed string.
# 3  
Old 06-23-2011
If I understand correctly, what that is doing is removing what you tell it to remove, am I right? If I am right, I can't use that, the damn file is 2149 lines, and it always changes.

If that's not what it's doing, can you show me how it would work with my example text?
# 4  
Old 06-23-2011
Well, a sed-ism for your case would be:
Code:
s/.*pattern1\(.*\)pattern2.*/\1/

but I do not play in PERL, being a sed or C or ksh guy. PERL may treat the string buffer as one line, or many, regardless of embedded characters. That is the 'like one line' solution, my first guess. Write a simple like the web site and try it out with a simple string that models your case, but simpler. The best knowledge is locally made.
# 5  
Old 06-23-2011
Well, sadly, it doesn't seem to work. I used some test text with the trim thing you linked to, did not work with newlines. look:
Code:
sub trim{
   my $string = shift;
   $string =~ s/.*real\(.*\)not.*/\1/g;
   return $string;
}
print "[".trim("My real name is not Deadman")."]\n";

And the output was
Code:
[name is]

Which worked perfectly. Sadly, next try:
Code:
sub trim{
   my $string = shift;
   $string =~ s/.*real\(.*\)not.*/\1/g;
   return $string;
}
print "[".trim("My\nreal\nname\nis\nnot\nDeadman")."]\n";

And the output was
Code:
[My
real
name
is
not
Deadman]

And just in case that was a special case, I tried it with what I needed, again, printed the entire thing.
# 6  
Old 06-24-2011
How about this
Code:
 
perl -0pe 's/.*(real.*not\n).*/\1/sg' input

# 7  
Old 06-24-2011
2 things - I wouldn't know how to go about including that in ONLY A PERL SCRIPT and the input is a variable, not a file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 Replies

2. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

3. Shell Programming and Scripting

Print between multiple patterns

Hello Gurus, I have a file this Dir Path 1 Connection pool="somename"; "DataSource Name"="DS name"; Password="pwd"; User Id="uid";some other fields Dir Path2 Password="pwd2"; User id="uid2"; Connection pool="somename2"; "datasource name"="DS name2";some other fields. Under each dir... (14 Replies)
Discussion started by: sirababu
14 Replies

4. Shell Programming and Scripting

Perl : to print the lines between two patterns

Hello experts, I have a text file from which I need to print all the lines between the patterns. Could anyone please help me with the perl script. names.txt ========= Badger Bald Eagle Bandicoot Bangle Tiger Barnacle Barracuda Basilisk Bass Basset Hound Beetle Beluga... (7 Replies)
Discussion started by: scriptscript
7 Replies

5. Shell Programming and Scripting

Print all lines between patterns

Hi Gurus, I have a requirement where I need to display all lines between 2 patterns except the line where the first pattern in it. I tried the following command using awk but it is printing all lines except the lines where the 2 patterns exist. awk '/TRANSF_/{ P=1; next } /Busy/ {exit} P'... (9 Replies)
Discussion started by: svajhala
9 Replies

6. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

7. Shell Programming and Scripting

Need to print between patterns AND a few lines before

I need to print out sections (varying numbers of lines) of a file between patterns. That alone is easy enough: sed -n '/START/,/STOP/' I also need the 3 lines BEFORE the start pattern. That alone is easy enough: grep -B3 START But I can't seem to combine the two so that I get everything between the... (2 Replies)
Discussion started by: Finja
2 Replies

8. Shell Programming and Scripting

To print certain patterns in a column

Hi, From my input files, I want to print $1, $2 and only certain pattern in $4 (EC). I use this code but it print all the words in $4 awk -F"\t" '$4 {print $1,$2,$4}I just want EC follows by the numbers in $4 The input file as follows:- Entry Entry name Status Names Q01284 ... (7 Replies)
Discussion started by: redse171
7 Replies

9. Shell Programming and Scripting

print all between patterns with special chars

Hi, I'm having trouble with awk print all characters between 2 patterns. I tried more then one solution found on this forum but with no success. Probably my mistakes are due to the special characters "" and "]"in the search patterns. Well, have a log file like this: logfile.txt ... (3 Replies)
Discussion started by: ginolatino
3 Replies

10. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies
Login or Register to Ask a Question