Sponsored Content
Top Forums Shell Programming and Scripting Shell script to find longest phrase Post 302290651 by stargazerr on Tuesday 24th of February 2009 12:26:49 AM
Old 02-24-2009
A phrase is a collection of n number of words that are the same. Could be anything from 2 to n. So for example in a sample text:

The quick brown fox jumped over the ugly dog
The quick brown fox is fast asleep in a corner.

In the above sample 'The quick brown fox' is a the phrase I am looking for, as it occurs twice in the text. In another sample

My name is John Doe
My name used to Alfred

The "phrase" here will be "My name" as that is what is consistent.

I hope that makes a bit more sense ? Smilie

Thanks
SG
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find the length of the longest line

Dear All, To find the length of the longest line from a file i have used wc -L which is giving the proper output... But the problem is AIX os does not support wc -L command. so is there any other way 2 to find out the length of the longest line using awk or sed ? Regards, Pankaj (1 Reply)
Discussion started by: panknil
1 Replies

2. Shell Programming and Scripting

c shell script help with find

Okie here is my problem, 1. I have a directory with a ton of files. 2. I want to first get an input on how many days ago the files were created. 3. I will take those files and put it into another file 4. Then I will take the last # from each line and subtract by 1 then diff the line from the... (1 Reply)
Discussion started by: bigboizvince
1 Replies

3. Shell Programming and Scripting

find PHRASE and PATH

I've got a script which finds *.txt files in directories and subdirectories after providing the path by the user and then searches in the files for phrase given by the user How to write script in such way that the paths to the found *.txt files and the phrase given by the user were both... (2 Replies)
Discussion started by: patrykxes
2 Replies

4. Shell Programming and Scripting

shell script: longest match from right?

Return the position of matched string from right, awk match can do from left only. e.g return pos 7 for search string "service" from "AA-service" or return the matched string "service", then caculate the string length. Thanks!. (3 Replies)
Discussion started by: honglus
3 Replies

5. Shell Programming and Scripting

Bash script find longest line/lines in several files

Hello everyone... I need to find out, how to find longest line or possibly lines in several files which are arguments for script. The thing is, that I tried some possibilities before, but nothing worked correctly. Example when i use: awk ' { if ( length > L ) { L=length ;s=$0 } }END{ print... (23 Replies)
Discussion started by: 1tempus1
23 Replies

6. Shell Programming and Scripting

Find longest string and print it

Hello all, I need to find the longest string in a select field and print that field. I have tried a few different methods and I always end up one step from where I need to be. Methods thus far: nawk '{if (length($1) > long) long=length($1); if(length($1)==long) print $1}' The above... (6 Replies)
Discussion started by: SEinT
6 Replies

7. Shell Programming and Scripting

How to find out the shell of the shell script?

Hello My question is: How to find out the shell of the shell script which we are running? I am writing a script, say f1.sh, as below: #!/bin/ksh echo "Sample script" From the first line, we can say this script will run in ksh. But, how can we prove it? Can we print anything inside... (6 Replies)
Discussion started by: guruprasadpr
6 Replies

8. Shell Programming and Scripting

Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through... Script that i am using is below : #!/bin/bash sum=0 for i in $@ do sum=$sum+$1 echo $sum shift done I am executing the script as... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

9. Shell Programming and Scripting

How to find a phrase and pull all lines that follow until the phrase occurs again?

I want to burst a report by using the page number value in the report header. Each section starts with *PAGE NO:* 1 Each section might have several pages, but the next section always starts back at 1. So I want to find the "*PAGE NO:* 1" value and pull all lines that follow until "*PAGE NO:* 1"... (4 Replies)
Discussion started by: Scottie1954
4 Replies

10. Shell Programming and Scripting

Help script shell find fichier

Hello, I am looking for a shell script that can 1- take as input a variable, like "server.cpu" 2- do a search for that variable in a directory that contains subdirectories. The search will start at the last subdirectory working up to the top level if I can not find the file 3-... (7 Replies)
Discussion started by: georg2014
7 Replies
IO::Capture::Overview(3pm)				User Contributed Perl Documentation				IO::Capture::Overview(3pm)

NAME
IO::Capture -- Overview of "IO::Capture" Module, and classes derived from it. DESCRIPTION
The modules in this distribution are designed to allow you to capture and process output sent to STDOUT and/or STDERR. I initial created the modules to use in building module tests. I wanted to be able to intentionally cause errors, and insure the module responded correctly. E.g., Call a class method without a required argument. Using IO::Capture keeps the user from seeing these inten- tional errors when running 'make test'. I have also found this useful on occasion in Perl Tk apps, where I wanted to capture output from a Perl module I was using. I could then capture, then put the text into a log or message window. Note: None of the modules currently distributed will capture from the 'system' Perl function, or the like. It could be done, but gener- ally, if you would like to capture from a system command, you don't need this module, just use the backticks operators. my $output = '/usr/bin/ls'; They are small, lightweight modules. Instead of designing in a lot of features, we designed it to be easily reusable and adaptable. A module can be quickly built, that incorporates custom methods, but reuses all existing features of one of the derived classes. See the sec- tion on "ADDING FEATURES" Or, if you need to change the actual capture mechanism, "WRITING YOUR OWN DERIVED CLASS". (Don't worry, it's a piece of cake) DERIVED CLASSES
There are several classes derived from "IO::Capture". IO::Capture::Stdout Module to capture "STDOUT" from program. See IO::Capture::Stdout. IO::Capture::Stderr Module to capture "STDERR" from program. See IO::Capture::Stderr. IO::Capture::ErrorMessages This method has been depreciated. The only difference between this one and Stderr.pm was the trap for WARN. I found it was fixed in 5.8 so just check in Stderr now. I.e., Just use Stderr now. It (Stderr) will detect what version of perl you are using, and act accordingly. The two ("IO::Capture::ErrorMessages" and "IO::Capture::Stderr") are currently identical, and "IO::Capture::ErrorMessages" will be removed in a future release. If you would like to add features to any of these, or build your own module using "IO::Capture" as a base, read on. ADDING FEATURES
If one of these modules takes care of your problem, install it and have fun! But let's say you would like to add a feature to one of the derived classes, say IO::Capture::Stdout. No need to re-write the whole mod- ule, just use it as the base, and write your one feature. Here is a somewhat simplified example. # # Example module to add a grep_it method # # Give your package a name package MyPackage; #use IO:Capture:Stdout as the base use base 'IO::Capture::Stdout'; #define your method sub grep_it { my $self = shift; my $string = shift; my @found_lines; # Making a ref to the array makes it easier to read :-) my $arrayref = @{$self->{'IO::Capture::messages'}}; for my $line (@$arrayref) { push @found_lines, $line if $line =~ /$string/; } return wantarray ? @found_lines : scalar(@found_lines); } 1; Using it in this script #!/usr/sbin/perl use strict; use warnings; use MyPackage; my $capture = MyPackage->new(); $capture->start; print "The quick brown fox jumped over ..."; print "garden wall"; print "The quick red fox jumped over ..."; print "garden wall"; $capture->stop; for my $line ($capture->grep_it("fox")) { print "$line "; } Results in $ grep_it The quick brown fox jumped over ... The quick red fox jumped over ... WRITING YOUR OWN DERIVED CLASS
Before starting your own sub-class, be sure to read through IO::Capture. Pay special attention to the internal methods that are only defined as abstract methods in "IO::Capture". For examples, look at the sub-classes included with this distribution. ("IO::Capture::Std- out", "IO:Capture::Stderr". You can start by copying one of these and using it as a template. They have the required private methods defined already, and you may very well be able to use them as is. Change any methods, and add any new ones, as needed. For example, here is a commented copy of "IO::Capture::Stderr". # # Example module using abstract class IO::Capture # # Change this to give your class it's own name package IO::Capture::Stderr; # Make IO::Capture the base class use base qw/IO::Capture/; # If using included utility module in '_start()' use IO::Capture::Tie_STDx; # Override the three abstract methods needed to make a valid # module. See IO::Capture manpage # 1) _start - Starts the data capture. Is run from public method # start(); # # 2) _retrieve_captured_text() - Move the captured text into the # object hash key, "IO::Capture::messages". Called by public method # # 3) _stop - Stop the data capture. Called by public method 'stop()' # after private method '_retrieve_captured_text()' returns. # sub _start { tie *STDERR, "IO::Capture::Tie_STDx"; } sub _retrieve_captured_text { my $self = shift; # making a reference to it makes it more readable ;-) my $messages = @{$self->{'IO::Capture::messages'}}; @$messages = <STDERR>; } sub _stop { untie *STDERR; return 1; } 1; Lets say you don't want to capture all the text. You just want to grab the lines that have the word "Error" in them. The only thing you need to change is _retrieve_captured_text. (Besides the package name) Something like: sub _retrieve_captured_text { my $self = shift; # making a reference to it makes it more readable ;-) my $messages = @{$self->{'IO::Capture::messages'}}; while (<STDERR>) { push @$messages, $_ if /error/i; } } Yes. You could do this easier by just using "IO::Capture::Stderr" as the base and overriding "_retrieve_captured_text" like in "ADDING FEA- TURES", but hey, we needed an easy example. :-) If you want your class to have arguments that users can pass in, just use the default "new()" method and have the arguments passed in as an anonymous array. See the "IO::Capture::Stderr" module for an example. BUGS
Please report bugs on http://rt.cpan.org/ CREDITS
Special thanks to James E Keenan for many bug fixes and tests he provided. AUTHOR
Mark Reynolds reynolds<at>sgi.com Note: "Change <at" to 'at' sign.> COPYRIGHT
Copyright (c) 2003-2005, Mark Reynolds. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. perl v5.8.8 2007-07-30 IO::Capture::Overview(3pm)
All times are GMT -4. The time now is 01:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy