Sponsored Content
Top Forums Shell Programming and Scripting how to find the shortest line which containing a key string? Post 302603626 by rangarasan on Thursday 1st of March 2012 01:59:38 PM
Old 03-01-2012
Hi,
Try this,
Code:
awk '/M0271857/{l=length($0);if(min==""){min=l;r=$0;}else{if(l<min){min=l;r=$0;}}}END{print r;}' file

Cheers,
Ranga:-)

Last edited by rangarasan; 03-01-2012 at 03:04 PM..
This User Gave Thanks to rangarasan For This Post:
 

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

To find largest and shortest word in eld

I got a file called Album in that there is list of songs i want to find the Longest and shortest song name in field 2 ie ($2).... Please help me with "awk" (2 Replies)
Discussion started by: Markwaugh
2 Replies

3. Shell Programming and Scripting

find the string in a line

Hello I have a abc.txt file which semicolon delimited. I need to read the first line of the abc.txt file where i need to extract specific string which at specific location. i.e first line of the abc.txt file is as below abc;123;xyz;345;678 my requirement is i need to get the 678. I was... (7 Replies)
Discussion started by: dsdev_123
7 Replies

4. Programming

Find a line containing a string.

Hello. I have a large file that contains a lot of gibberish and also a lot of http addresses. How can i read the file, take out the http addresses, and write each one of them on one line each into another file? It looks something like this. ... (1 Reply)
Discussion started by: cbreiny
1 Replies

5. Shell Programming and Scripting

Find line containing string in a file.

Hello. I have a large file that contains a lot of gibberish and also a lot of http addresses. How can i read the file, take out the http addresses, and write each one of them on one line each into another file? It looks something like this. ... (7 Replies)
Discussion started by: cbreiny
7 Replies

6. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

7. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

8. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

9. Homework & Coursework Questions

Need help how to search for shortest line from a file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to write a program that have to read every standard input then print out the line number and the content of... (10 Replies)
Discussion started by: scopiop
10 Replies

10. Shell Programming and Scripting

Find a string and its position in a line from another string

Hello guys, would you please help me with this? this is the line inside a file: first line Something Today YYDDPPSVXIPYYY0XXXOFFS00000000000? I'd like to find the position of string XXX from string PYYY In the example above XXX starts from 6th position from PYYY desired... (4 Replies)
Discussion started by: netrom
4 Replies
LEVENSHTEIN(3)								 1							    LEVENSHTEIN(3)

levenshtein - Calculate Levenshtein distance between two strings

SYNOPSIS
int levenshtein (string $str1, string $str2) DESCRIPTION
int levenshtein (string $str1, string $str2, int $cost_ins, int $cost_rep, int $cost_del) The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete to transform $str1 into $str2. The complexity of the algorithm is O(m*n), where n and m are the length of $str1 and $str2 (rather good when compared to simi- lar_text(3), which is O(max(n,m)**3), but still expensive). In its simplest form the function will take only the two strings as parameter and will calculate just the number of insert, replace and delete operations needed to transform $str1 into $str2. A second variant will take three additional parameters that define the cost of insert, replace and delete operations. This is more general and adaptive than variant one, but not as efficient. PARAMETERS
o $str1 - One of the strings being evaluated for Levenshtein distance. o $str2 - One of the strings being evaluated for Levenshtein distance. o $cost_ins - Defines the cost of insertion. o $cost_rep - Defines the cost of replacement. o $cost_del - Defines the cost of deletion. RETURN VALUES
This function returns the Levenshtein-Distance between the two argument strings or -1, if one of the argument strings is longer than the limit of 255 characters. EXAMPLES
Example #1 levenshtein(3) example <?php // input misspelled word $input = 'carrrot'; // array of words to check against $words = array('apple','pineapple','banana','orange', 'radish','carrot','pea','bean','potato'); // no shortest distance found, yet $shortest = -1; // loop through words to find the closest foreach ($words as $word) { // calculate the distance between the input word, // and the current word $lev = levenshtein($input, $word); // check for an exact match if ($lev == 0) { // closest word is this one (exact match) $closest = $word; $shortest = 0; // break out of the loop; we've found an exact match break; } // if this distance is less than the next found shortest // distance, OR if a next shortest word has not yet been found if ($lev <= $shortest || $shortest < 0) { // set the closest match, and shortest distance $closest = $word; $shortest = $lev; } } echo "Input word: $input "; if ($shortest == 0) { echo "Exact match found: $closest "; } else { echo "Did you mean: $closest? "; } ?> The above example will output: Input word: carrrot Did you mean: carrot? SEE ALSO
soundex(3), similar_text(3), metaphone(3). PHP Documentation Group LEVENSHTEIN(3)
All times are GMT -4. The time now is 02:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy