![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Substring in shell script | jyotib | Shell Programming and Scripting | 5 | 01-16-2008 03:58 PM |
| Extracting a substring starting from last occurance of a string/character | krramkumar | Shell Programming and Scripting | 2 | 12-18-2007 11:16 PM |
| help for shell script of finding shortest substring from given string by user | pankajd | Shell Programming and Scripting | 1 | 11-22-2007 08:27 AM |
| problem extracting substring in korn shell | nashrul | UNIX for Dummies Questions & Answers | 3 | 08-14-2007 11:45 PM |
| Substring in C shell script? | dinodash | Shell Programming and Scripting | 0 | 03-20-2005 09:59 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
shell script for extracting out the shortest substring from the given starting and en
hi all,
i need an urgent help for writing a shell script which will extract out and print a substring which is the shortest substring from the given string where first and last character of that substring will be given by the user. for e.g. if str="abcdpqracdpqaserd" now if the user gives 'a' and 'd' as the first and last character of the substringi.e. command line arguments.this should extract out acd as the shortest string. please give simple solution to this. |
| Forum Sponsor | ||
|
|
|
|||
|
awk
Hi,
If really took my much efforts. I have tested it for many cases. And they are all ok. Hope this is right on your target. input: Code:
abcdpqracdpqaserd abcdpqracdpqaserd abcdpqracdpqaserd Code:
acd acd acd Code:
acdp acdp acdp Code:
abcdpqr abcdpqr abcdpqr Code:
read a
read b
sed -e "s/$a[^$b]*$b/|&|/g" a > temp_a
sed 's/^|//' temp_a > temp_b
nawk -v st=$a -v ed=$b 'BEGIN{
FS="|"
}
{
for(i=1;i<=NF;i++)
{
str=sprintf("b%s",$i)
if(index(str,"a")==2)
{
if(tmp=="")
{
tmp=$i
}
else
{
if (length($i)<length(tmp))
tmp=$i
}
}
}
print tmp
}
' temp_b
|
|
||||
|
With GNU Awk:
Code:
awk 'NF>1&&$0=(FS $NF RT){
if(length<min){
min=length;rec=$0}
}END{
print rec
}' FS="$start" RS="$end" min=9^9 filename
Code:
$ cat file
abcdpqracdpqaserd
$ start=a
$ end=d
$ awk 'NF>1&&$0=(FS $NF RT){
if(length<min){
min=length;rec=$0}
}END{
print rec
}' FS="$start" RS="$end" min=9^9 file
acd
$ start=a
$ end=p
$ awk 'NF>1&&$0=(FS $NF RT){
if(length<min){
min=length;rec=$0}
}END{
print rec
}' FS="$start" RS="$end" min=9^9 file
acdp
$ start=a
$ end=r
$ awk 'NF>1&&$0=(FS $NF RT){
if(length<min){
min=length;rec=$0}
}END{
print rec
}' FS="$start" RS="$end" min=9^9 file
aser
|
|
||||
|
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);
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 ) See the man pages for: Code:
perlre Perl regular expressions, the rest of the story perlreref Perl regular expressions quick reference |
| Tags |
| regex, regular expressions |
| Thread Tools | |
| Display Modes | |
|
|