Sponsored Content
Top Forums Shell Programming and Scripting Find and select complete paragraph Post 302962900 by Aia on Sunday 20th of December 2015 04:28:12 PM
Old 12-20-2015
Quote:
Originally Posted by anushree.a
I tried code suggested by Aia and its working as per expectations.
However, I would like to run this perl in loop as I have around 830 different patterns like "Permanent".


Code:
cat filter.pl
#!/usr/bin/perl
use strict;
use warnings;

my $sep = $/ = "== :: ==\n";
my $pattern = `$1`;
my $hat = 0;

while(<>) {
if(/$pattern/){
print $sep if $hat == 0 and ++$hat;
print;
 }
}

and made following loop.

Code:
for service in `cat input_patterns`
do
echo ${service}
perl filter.pl ${service} file>>pattern.out
done

however, its not working.
I am sure I am doing something wrong.
Kindly suggest.
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for full-line and multi-line sample input, output, and code segments.
This is not tested, however, if you want to use the Perl script in that way you need a different modification that what I highlighted in red.

Code:
#!/usr/bin/perl
use strict;
use warnings;

my $pattern = shift;
my $sep = $/ = "== :: ==\n";
my $hat = 0;


while(<>) {
    if(/$pattern/){
        print $sep if $hat == 0 and ++$hat;
        print;
    }
}

And then you can use the following shell script

Code:
#!/bin/bash

while read service; do
    perl filter.pl "$service" file.txt >> pattern.out
done < input_patterns

Of course, that might be slow due to all the times the binary perl gets called, and the opening, appending and closing of pattern.out. Your mileage may vary there.

Here's a Perl script that might work alone.
Again, it is not tested but you can try with just a portion of your 800 plus patterns input_patterns. It assumes one pattern per line.

Code:
#!/usr/bin/perl
use strict;
use warnings;

my $pattern;
my $pattern_source = shift;
{
    local $/ = undef;
    open my $fh, '<', $pattern_source or die "$!\n";
    $pattern = <$fh>;
    $pattern =~ s/\R(?!$)/\|/g;
    close $fh;
}
my $sep = $/ = "== :: ==\n";
my $hat = 0;
while(<>) {
    if(/$pattern/){
        print $sep if $hat == 0 and ++$hat;
        print;
    }
}

Use as perl filter.pl input_patterns april_2015.txt > pattern.out
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bold the paragraph

Hi, I have a file with multiple paragraph. I want to look for some word and make that paragraph bold. How can I do that? Thanks, Karthik (3 Replies)
Discussion started by: caprikar
3 Replies

2. UNIX for Advanced & Expert Users

how to find complete path of a file in unix

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to capture... (1 Reply)
Discussion started by: yahoo!
1 Replies

3. UNIX for Dummies Questions & Answers

how to find complete path of a file in unix

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to capture... (5 Replies)
Discussion started by: yahoo!
5 Replies

4. UNIX for Dummies Questions & Answers

BASH complete-filename & menu-complete together

Hi, Does anyone know how to make BASH provide a list of possible completions on the first tab, and then start cycling through the possibilites on the next tab? Right now this is what I have in my .bashrc: bind "set show-all-if-ambiguous on" bind \\C-o:menu-complete This allows... (0 Replies)
Discussion started by: Mithu
0 Replies

5. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

6. Shell Programming and Scripting

How to find complete file names in UNIX if i know only extention of file

Suppose I have a file which contains other file names with some extention . text file containt gdsds sd8ef g/f/temp_temp.sum yyeta t/unix.sum ghfp hrwer h/y/test.text.dat if then.... I want to get the complete file names, like for above file I should get output as temp_temp.sum... (4 Replies)
Discussion started by: panchal
4 Replies

7. UNIX for Dummies Questions & Answers

Unable to execute the complete cmd - using find command

Hi, I'm unable to execute the below command completely ; it's not allowing me to type the complete command. It is allowing till "xargs" and i cannot even press enter after that. I'm using Solaris. Let me know if anything needs to be added so as to execute the complete command. Appreciate... (12 Replies)
Discussion started by: venkatesht
12 Replies

8. Shell Programming and Scripting

How to grep paragraph?

Hi, I have A file like this: >Contig1 AAAAAAATTTTTTCCCAATATATGAT ATATATAEATATATAT >Contig2 AAAAAAATTTTTTCCCAATATATGAT ATATATAEAATTTTTAATTTTTTCCCA ATCCCAAATATATAT >Contig3 AAAAAAATTTTTTCCCAATATATGAT ATATATAEAATTTTTAATTTTTTCCCA ATCCCAAATAAATTTTTTCCCAATAT ATGATATATATAEAATTTTTAATTTTT... (3 Replies)
Discussion started by: the_simpsons
3 Replies

9. UNIX for Advanced & Expert Users

Find command takes too long to complete

Hi, Below is my find command find /opt/app/websphere -name myfolder -perm -600 | wc -l At time it even takes 20 mins to complete. my OS is : SunOS mypc 5.10 Generic_150400-09 sun4v sparc SUNW,T5440 (10 Replies)
Discussion started by: mohtashims
10 Replies

10. UNIX for Dummies Questions & Answers

Extract paragraph that contains a value x<-30

I am using OSX. I have a multi-mol2 file (text file with coordinates and info for several molecules). An example of two molecules in the file is given below for molecule1 and molecule 2. The total file contains >50,000 molecules. I would like to extract out and write to another file only the... (2 Replies)
Discussion started by: Egy
2 Replies
All times are GMT -4. The time now is 10:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy