Sponsored Content
Top Forums Shell Programming and Scripting Perl : to print the lines between two patterns Post 302814675 by scriptscript on Thursday 30th of May 2013 03:55:59 AM
Old 05-30-2013
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 Whale
Bird
Big-horned sheep
Bird of paradise
Be
Billy goat bandad
indobill
From the above file...

I will be entering any of the 2 names and will be printing all the lines in between the patterns.

$source= "Badger"
$dest= "goat"

I have to all the lines in between Badger and goat.

output should be as below..

Badger
Bald Eagle
Bandicoot
Bangle Tiger
Barnacle
Barracuda
Basilisk
Bass
Basset Hound
Beetle
Beluga Whale
Bird
Big-horned sheep
Bird of paradise
Be
Billy goat

Could anyone please help with the perl script.

Regards,
Giridhar
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

print lines between two patterns in unix

Detroit Chicago Newyork Battlecreek Jackson Brooklyn How would I print only lines match between Detroit and Brooklyn used awk ? I don't want print Detroit and Brooklyn output should be : Chicago Newyork Battlecreek Jackson Thanks Jhonny (2 Replies)
Discussion started by: jhonnyrip
2 Replies

2. Shell Programming and Scripting

Print lines between two repetitive patterns

Hi users I have one file which has number of occurrence of one pattern examples Adjustmenttype,11 xyz 10 dwe 9 abd 13 def 14 Adjustmenttype,11 xyz 24 dwe 34 abd 35 def 11 nmb 12 Adjustmenttype, not eleven .... ... ... (2 Replies)
Discussion started by: eranmoh
2 Replies

3. Shell Programming and Scripting

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 My real name is not DeadmanAnd I need the output to be this, by printing between "real" and "not" name is or including the two... (10 Replies)
Discussion started by: killer54291
10 Replies

4. Shell Programming and Scripting

print lines between 2 matching patterns

Hi Guys, I have file like below, I want to print all lines between test1231233 to its 10 occurrence(till line 41) test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq23 test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq123131 (3 Replies)
Discussion started by: jagnikam
3 Replies

5. 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

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

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

8. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

9. 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

10. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies
IO::File(3pm)						 Perl Programmers Reference Guide					     IO::File(3pm)

NAME
IO::File - supply object methods for filehandles SYNOPSIS
use IO::File; $fh = new IO::File; if ($fh->open("< file")) { print <$fh>; $fh->close; } $fh = new IO::File "> file"; if (defined $fh) { print $fh "bar "; $fh->close; } $fh = new IO::File "file", "r"; if (defined $fh) { print <$fh>; undef $fh; # automatically closes the file } $fh = new IO::File "file", O_WRONLY|O_APPEND; if (defined $fh) { print $fh "corge "; $pos = $fh->getpos; $fh->setpos($pos); undef $fh; # automatically closes the file } autoflush STDOUT 1; DESCRIPTION
"IO::File" inherits from "IO::Handle" and "IO::Seekable". It extends these classes with methods that are specific to file handles. CONSTRUCTOR
new ( FILENAME [,MODE [,PERMS]] ) Creates an "IO::File". If it receives any parameters, they are passed to the method "open"; if the open fails, the object is destroyed. Otherwise, it is returned to the caller. new_tmpfile Creates an "IO::File" opened for read/write on a newly created temporary file. On systems where this is possible, the temporary file is anonymous (i.e. it is unlinked after creation, but held open). If the temporary file cannot be created or opened, the "IO::File" object is destroyed. Otherwise, it is returned to the caller. METHODS
open( FILENAME [,MODE [,PERMS]] ) open( FILENAME, IOLAYERS ) "open" accepts one, two or three parameters. With one parameter, it is just a front end for the built-in "open" function. With two or three parameters, the first parameter is a filename that may include whitespace or other special characters, and the second parameter is the open mode, optionally followed by a file permission value. If "IO::File::open" receives a Perl mode string (">", "+<", etc.) or an ANSI C fopen() mode string ("w", "r+", etc.), it uses the basic Perl "open" operator (but protects any special characters). If "IO::File::open" is given a numeric mode, it passes that mode and the optional permissions value to the Perl "sysopen" operator. The permissions default to 0666. If "IO::File::open" is given a mode that includes the ":" character, it passes all the three arguments to the three-argument "open" operator. For convenience, "IO::File" exports the O_XXX constants from the Fcntl module, if this module is available. binmode( [LAYER] ) "binmode" sets "binmode" on the underlying "IO" object, as documented in "perldoc -f binmode". "binmode" accepts one optional parameter, which is the layer to be passed on to the "binmode" call. NOTE
Some operating systems may perform "IO::File::new()" or "IO::File::open()" on a directory without errors. This behavior is not portable and not suggested for use. Using "opendir()" and "readdir()" or "IO::Dir" are suggested instead. SEE ALSO
perlfunc, "I/O Operators" in perlop, IO::Handle, IO::Seekable, IO::Dir HISTORY
Derived from FileHandle.pm by Graham Barr <gbarr@pobox.com>. perl v5.12.1 2010-04-26 IO::File(3pm)
All times are GMT -4. The time now is 10:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy