Sponsored Content
Top Forums Shell Programming and Scripting shell script for extracting out the shortest substring from the given starting and en Post 302141524 by drl on Friday 19th of October 2007 12:59:21 PM
Old 10-19-2007
Hi.

I like the solution from aigles. I don't see one yet on perl.

The perl RE syntax has special features for the shortest match. Here is the entire code, along with diagnostic code, minimal argument processing, etc:
Code:
#!/usr/bin/perl

# @(#) p1       Demonstrate non-greedy matching perl RE syntax.

use warnings;
use strict;

my ($debug);
$debug = 0;
$debug = 1;

my ($lines) = 0;

my ($usage) = "usage: $0 first last\n";
my ($first) = shift || die "$usage";
my ($last)  = shift || die "$usage";

my ($string);

while (<>) {
  print " Bounds on this search: $first, $last\n" unless $lines;
  $lines++;
  chomp;
  print "\n";
  print " Initial string = \"$_\"\n";
  if (/($first.*?$last)/) {
    $string = $1;
    print " Shortest substring = \"$string\"\n";
  }
  else {
    print STDERR " No substring found, continuing.\n";
  }
}

print STDERR " ( Lines read: $lines )\n";

exit(0);

Running this on your test line and a few others in file data1:
Code:
% ./p1 a d data1
 Bounds on this search: a, d

 Initial string = "abcdpqracdpqaserd"
 Shortest substring = "abcd"

 Initial string = "abc"
 No substring found, continuing.

 Initial string = "abcdddd"
 Shortest substring = "abcd"
 ( Lines read: 3 )

The heart of the match is in these characters .*?

See the man pages for:
Code:
perlre              Perl regular expressions, the rest of the story
perlreref           Perl regular expressions quick reference

for details ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substring in C shell script?

i am a new user of C-shell script. I want to know can i create a substring in a string. That means when i got a variable $input = "it is number 2" I want to get the "2" to be another variable. Can i do that in C-shell and how to ? Thank you so much dinodash (0 Replies)
Discussion started by: dinodash
0 Replies

2. UNIX for Dummies Questions & Answers

problem extracting substring in korn shell

hi all, I have read similiar topics in this board, but i didn' t find the posting which is the same with the problem i face.. I try to extract string from the end. i try to do this: num=abcdefghij num2=${num:-5} echo $num2 #this should print the last 5 characters (fghij) but it doesn;t... (3 Replies)
Discussion started by: nashrul
3 Replies

3. UNIX for Dummies Questions & Answers

Substring in Shell Script

Hi I'm new to Shell scripting. Someone please help me in extracting a portion of string from a file. Eg: I got a file like, Readme.txt and has the following name value pairs input1 : /homes/input1/ input2 : /homes/input2/ ... ... When I give the parameter input1, the value... (3 Replies)
Discussion started by: smartbuddy
3 Replies

4. Shell Programming and Scripting

help for shell script of finding shortest substring from given string by user

please give me proper solution for finding a shortest substring from given string if string itself and first char and last char of that substr are also given by user if S="dpoaoqooroo" and FC="o" and LC="o",then shortest substr is "oo" and rest of the string is "dpoaoqroo" i have code but it is... (1 Reply)
Discussion started by: pankajd
1 Replies

5. Shell Programming and Scripting

Extracting a substring starting from last occurance of a string/character

Hi All, This is Ram. I'm new to this forum & new to shell scripts as well. I've a requirement in which I want to extract a substring from a given string based on last occurance of a character. for eg. I have a string of a file name with absolute path like... (2 Replies)
Discussion started by: krramkumar
2 Replies

6. Shell Programming and Scripting

Substring in shell script

I need a help in getting substring of each line in input file. I am writing a script that will read a file from a directory on daily basis, I mean everyday a new file will be stored in this directory, it will replace old file. I have to read contents of this file, the contents will be as... (5 Replies)
Discussion started by: jyotib
5 Replies

7. Shell Programming and Scripting

using substring in shell script

This is the data I am having in a file Just for sample I have given 3 records. The file which I am having consists of n number of records. ABC123 10 01/02/2008 2008-01-03-00.00.00.000000 DYUU 22 02/03/2008 2008-01-04-00.00.00.000000 RF33 88 03/05/2008 2008-01-05-00.00.00.000000 ... (24 Replies)
Discussion started by: kmanivan82
24 Replies

8. Shell Programming and Scripting

extracting substring from a file name

hi i need to name a file with a substring of a another file name. i.e. if the old filename is abc.txt , the new filename should be abc_1.txt i should get the substring of the file name and then name the new one please let me know how to do it (4 Replies)
Discussion started by: adityamahi
4 Replies

9. Shell Programming and Scripting

Extracting substring from string

Hi awk and sed gurus, Please help me in the following. I have the following entries in the file ABCDErules AbHDPrules ABCrules -- -- and other entries in the file. Now, I want to extract from the file that contain entries for *rules and process it separately. How can i do it... (6 Replies)
Discussion started by: sdosanjh
6 Replies

10. Shell Programming and Scripting

Extracting substring

Hi, I have string in variable like '/u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010' and i want to conevrt this string into only "DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010" (i.e file name). Is there any command to extracting string in some part ?(rather than whole path)? ... (5 Replies)
Discussion started by: shyamu544
5 Replies
Data::Munge(3pm)					User Contributed Perl Documentation					  Data::Munge(3pm)

NAME
Data::Munge - various utility functions SYNOPSIS
use Data::Munge; my $re = list2re qw/foo bar baz/; print byval { s/foo/bar/ } $text; foo(mapval { chomp } @lines); print replace('Apples are round, and apples are juicy.', qr/apples/i, 'oranges', 'g'); print replace('John Smith', qr/(w+)s+(w+)/, '$2, $1'); DESCRIPTION
This module defines a few generally useful utility functions. I got tired of redefining or working around them, so I wrote this module. Functions list2re LIST Converts a list of strings to a regex that matches any of the strings. Especially useful in combination with "keys". Example: my $re = list2re keys %hash; $str =~ s/($re)/$hash{$1}/g; byval BLOCK SCALAR Takes a code block and a value, runs the block with $_ set to that value, and returns the final value of $_. The global value of $_ is not affected. $_ isn't aliased to the input value either, so modifying $_ in the block will not affect the passed in value. Example: foo(byval { s/!/?/g } $str); # Calls foo() with the value of $str, but all '!' have been replaced by '?'. # $str itself is not modified. mapval BLOCK LIST Works like a combination of "map" and "byval"; i.e. it behaves like "map", but $_ is a copy, not aliased to the current element, and the return value is taken from $_ again (it ignores the value returned by the block). Example: my @foo = mapval { chomp } @bar; # @foo contains a copy of @bar where all elements have been chomp'd. # This could also be written as chomp(my @foo = @bar); but that's not # always possible. submatches Returns a list of the strings captured by the last successful pattern match. Normally you don't need this function because this is exactly what "m//" returns in list context. However, "submatches" also works in other contexts such as the RHS of "s//.../e". replace STRING, REGEX, REPLACEMENT, FLAG replace STRING, REGEX, REPLACEMENT A clone of javascript's "String.prototype.replace". It works almost the same as "byval { s/REGEX/REPLACEMENT/FLAG } STRING", but with a few important differences. REGEX can be a string or a compiled "qr//" object. REPLACEMENT can be a string or a subroutine reference. If it's a string, it can contain the following replacement patterns: $$ Inserts a '$'. $& Inserts the matched substring. $` Inserts the substring preceding the match. $' Inserts the substring following the match. $N (where N is a digit) Inserts the substring matched by the Nth capturing group. ${N} (where N is one or more digits) Inserts the substring matched by the Nth capturing group. Note that these aren't variables; they're character sequences interpreted by "replace". If REPLACEMENT is a subroutine reference, it's called with the following arguments: First the matched substring (like $& above), then the contents of the capture buffers (as returned by "submatches"), then the offset where the pattern matched (like "$-[0]", see "@-" in perlvar), then the STRING. The return value will be inserted in place of the matched substring. Normally only the first occurrence of REGEX is replaced. If FLAG is present, it must be 'g' and causes all occurrences to be replaced. AUTHOR
Lukas Mai, "<l.mai at web.de>" COPYRIGHT &; LICENSE Copyright 2009-2011 Lukas Mai. This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information. perl v5.12.4 2011-08-03 Data::Munge(3pm)
All times are GMT -4. The time now is 01:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy