Printing between 2 matches with Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing between 2 matches with Perl
# 1  
Old 12-16-2009
Printing between 2 matches with Perl

Quote:
Originally Posted by skmdu
Code:
$perl -e 'while(<>){  exit if(/DISPLAY/); print $_; }' < test

This perl one liner will print the lines from the file called test.. If it gets the string DISPLAY, it just exit from the execution. Hope this helps you.
Can we please modify this perl one-liner to print lines between pattern1 and pattern2 in a file?

Currently it prints lines till pattern2.

Last edited by pludi; 12-16-2009 at 03:26 AM..
# 2  
Old 12-16-2009
Try this ....


Code:
perl -nle 'if ( $_ =~/start_pattern/ || $x==1) { $x=1 ; print ; $x=0 if (/END_PAttern/) }'  filename


Last edited by pludi; 12-16-2009 at 06:06 AM.. Reason: code tags, please...
# 3  
Old 12-16-2009
Do you want to include the start and end pattern? If so:
Code:
perl -nle 'print if /start_pattern/../end_pattern/' yourfile

# 4  
Old 12-16-2009
Try this as well

sed -n -e '/pattern1/,/pattern2/p' <filename>
# 5  
Old 12-16-2009
Not an one liner, but simple implementation for this case.

Code:
$flag = 0;

while (<DATA>)  {
    if ( $_ =~ /(.*)end_pattern/ )  {
        print "$1\n";
        $flag = 0;
    }
    print if ( $flag );
    if ( $_ =~ /start_pattern(.*)/ )  {
        print "$1\n";
        $flag = 1;
    }    
}

__DATA__
sdkfj sdfklj
sdfjk start_pattern data after pattern
sdkfj sdfklj 
sdflkj sdfkjsd klfjsd klfj
sdflkjsd fkljsd fkl
sdklfj end_pattern sdlfkj

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep -w not printing exact matches

Dear All, Here is my input TAACGCACTTGCGGCCCCGGGATAAAAAAAAAAAAAAAAAAAAATGGATT NAGAGGGACGGCCGGGGGCATAAAAAAAAAAAAAAAAAAAAAGGGATTTC NGGGTTTTAAGCAGGAGGTGTCAAAAAAAAAAAAAAAAAAAAAGGGATTT NTGGAACCTGGCGCTAGACCAAAAAAAAAAAAAAAAAAAATGGATTTTTG ATACTTACCTGGCAGGGGAGATACCATGATCAATAAAAAAAAAAAAAAAA... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

3. Shell Programming and Scripting

Perl: Counting matches

Hi, A perl newbie here so pretty sure it's something simple. Trying to figure out how to count matches with perl pattern matching. The following script opens a text data file and finds lines containing "PORT:" and I'd like to count how many of these are found. Any ideas? open(LOG,"<... (3 Replies)
Discussion started by: hdefjunkie
3 Replies

4. Shell Programming and Scripting

help with perl database printing

Hey guys i am using perl and trying to pull a list of books from a database and then populate the list in a separate TT2 file. When the list is generated there should be 39 book names. When I do the foreach statement in my tt2 below, the first statement gives me 39 Array(random number) and the... (1 Reply)
Discussion started by: Joey12
1 Replies

5. Shell Programming and Scripting

Perl line count if it matches a pattern

#!/usr/bin/perl use Shell; open THEFILE, "C:\galileo_integration.txt" || die "Couldnt open the file!"; @wholeThing = <THEFILE>; close THEFILE; foreach $line (@wholeThing){ if ($line =~ m/\\0$/){ @nextThing = $line; if ($line =~ s/\\0/\\LATEST/g){ @otherThing =... (2 Replies)
Discussion started by: nmattam
2 Replies

6. Shell Programming and Scripting

[Perl] Printing - Scalars

Hey Guys, I have some weird problem with printing scalars ... When I'm executing script both are printing in terminal ... But only one is printed to the file ? I don't know whats going on .. :) Btw .. I'm noobie :) took me lots of time to put this simple script together :) Thank you... (3 Replies)
Discussion started by: NDxiak
3 Replies

7. Shell Programming and Scripting

Perl printing error

Hi Scripting Gurus, I am new bee in perl, and trying to write a script which must print the free disk space availability of C and E drives. Please advice. Here is the script snippet and expected output: #!/usr/bin/perl use CGI qw/:html3 :standard/; $spaceuselog =... (4 Replies)
Discussion started by: ccsaviour
4 Replies

8. Shell Programming and Scripting

Matches and mismatches in perl

When we give an input sequence , the program should match with the pattern and give the matches and mismatches in the output. i will give you 2 small examples. if you cant get it pls let me know. i will try to give a clear idea. example 1: $a=APPLE; # let it be a pattern... (0 Replies)
Discussion started by: srisha
0 Replies

9. Shell Programming and Scripting

how to get the pos() of 2 str matches in one loop in perl

Hello all i have this simple loop that gets me every time the match of "<#" in my string something like that : my $str ="gggg<#nnnnn#>kkkk<#ssss#>llllll"; while($str =~m/<#/g){ print pos($str); } but now i like to get another pos in the same loop iteration , i will like to get the... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question