shell script for extracting out the shortest substring from the given starting and en


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script for extracting out the shortest substring from the given starting and en
# 15  
Old 10-20-2007
Another awk solution Smilie :
Code:
awk -v string="abcdpqracdpqaserd" \
    -v start="a"                  \
    -v end="d"                    \
    '
    BEGIN{
       regex = start "[^" end "]*" end;
       min_length = length(string) + 1;
       while (match(string,regex)) {
          if (RLENGTH < min_length) {
             min_length = RLENGTH;
             shortest   = substr(string, RSTART, RLENGTH);
          }
          string = substr(string, RSTART+1);
       }
       print shortest;
    }
    '

Jean-Pierre.
# 16  
Old 10-20-2007
Quote:
Originally Posted by aigles
Another awk solution Smilie :
Code:
awk -v string="abcdpqracdpqaserd" \
    -v start="a"                  \
    -v end="d"                    \
    '
    BEGIN{
       regex = start "[^" end "]*" end;
       min_length = length(string) + 1;
       while (match(string,regex)) {
          if (RLENGTH < min_length) {
             min_length = RLENGTH;
             shortest   = substr(string, RSTART, RLENGTH);
          }
          string = substr(string, RSTART+1);
       }
       print shortest;
    }
    '

Jean-Pierre.
... of course Smilie, the array was completely unnecessary,
thanks, Jean-Pierre, for pointing it out.
# 17  
Old 11-22-2007
Power

Quote:
Originally Posted by summer_cherry
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

output (start:a end:d):
Code:
acd
acd
acd

output (start:a endSmilie):
Code:
acdp
acdp
acdp

output (start:a end:r):
Code:
abcdpqr
abcdpqr
abcdpqr

code:
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


hi,
thank you for the code..
but this do not give correct result i.e u have not properly getting my question.
e.g.for input string as "abcdpqracdpqaserd",and first character is "a" and last char is "r",then shortest substring is "aser" and not "abcdpqr"so help me to generate such code..

Last edited by pankajd; 11-22-2007 at 02:04 AM..
# 18  
Old 03-10-2008
Question stream filter

Hello all, Please I need your help to stream out a particular FS (-)in this situation. There are several (-) in the output result I do get as you can notice in the excerpt below.

|0-0|filter -pstf |6464| 332558| 0| 41|Feb 20 01:56| 0| 0| 0|

|0-0|regular-styl131 |2794| 330622| 0| 41|Feb 20 01:56| 0| 0| 257|

|0-0|msnwr-1-0-styl131 | 38| 333886| 0| 41|Feb 20 01:56| 0| 0| 0|

|0-0|past-1-0-lgl131 | 150| 324889| 0| 101|Feb 20 01:56| 0| 35| 0|

|0-0|tns-sty131 |1315| 333811| 0| 11|Feb 20 01:56| 0| 0| 0|

--TOTAL--------------------------------------79-------------------896--10704686-620-

--TOTAL--------------------------------------795-------------------1596--10704686-5678-

The issue is I need to filter only the last values in the line TOTAL ( the coloured values). The number of characters are not predictable as the values are constantly changing.

How can I write a shell command that can print out only these values.

Thanks

Olusola
# 19  
Old 03-10-2008
Code:
awk '/^--TOTAL/{print $(NF-1)}' FS="-" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question