Perl print between 2 patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl print between 2 patterns
# 8  
Old 06-24-2011
Hi.

Using getmmg code, modifying and comparing:
Code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate feature (minimal).

use strict;
use warnings;
my ( $s1, $s2, $original );

print " Hello, world from perl minimal.\n";

$original = "My\nreal\nname\nis\nnot\nDeadman";
$s1       = $original;
print "[" . trim1($s1) . "]\n";

$s2 = $original;
print "[" . trim2($s2) . "]\n";

sub trim1 {
  my $string = shift;

  # $string =~ s/.*real\(.*\)not.*/\1/g;
  $string =~ s/.*real(.*)not.*/$1/g;
  return $string;
}

sub trim2 {
  my $string = shift;

  # $string =~ s/.*real\(.*\)not.*/\1/sg;
  $string =~ s/.*real(.*)not.*/$1/sg;
  return $string;
}

# print "[".trim("My\nreal\nname\nis\nnot\nDeadman")."]\n";

exit(0);

producing:
Code:
% ./p1
 Hello, world from perl minimal.
[My
real
name
is
not
Deadman]
[
name
is
]

Best wishes ... cheers, drl
# 9  
Old 06-24-2011
Well, you are right that newlines are not considered . food in PERL like this. You could pull the lines one at a time, look for the first pattern, trim it, start printing and looking for the second pattern, when you find it, trim, print and stop, but that seems harder than should be necessary. Maybe there is a way to get the right substring in one fell swoop.

Else, you could shell out to sed, which is cheating.

This looks close: Red Antigua - Perl trim function found with trim string PERL lines - Google Search

This would work if we had the offset: Perl substr Function

It looks like index can tell us the offset: Perl String Functions

Damn, I am learning PERL.
# 10  
Old 06-24-2011
drl, that works perfectly, thank you.
# 11  
Old 06-27-2011
More code tha something like: a=index string A in string X; b=index string B in string X; substr( X, a + length A, b - a - length A )
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