how to find the shortest line which containing a key string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find the shortest line which containing a key string?
# 1  
Old 03-01-2012
how to find the shortest line which containing a key string?

hi all,

suppose a key string: M0271857

and to find all lines containing this key string in a text file

which returns multiple lines

but i only want the shortest one

is there a way to do that?

thanks so much!
# 2  
Old 03-01-2012
Code:
awk '/M0271857/ && ((!L) || (length(L)>length($0))) { L=$0 } END { print L }' datafile

Meaning:

Code:
/M0271857/ # for all lines where your string matches
&& ((!L) || (length(L)>length($0))) # ...and L is undefined or longer than the line
{ L=$0 } # Set L to the entire line
END { print L } # After all lines are processed, print the shortest one found

This User Gave Thanks to Corona688 For This Post:
# 3  
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:
# 4  
Old 03-01-2012
Quote:
Originally Posted by Corona688
Code:
awk '/M0271857/ && ((!L) || (length(L)>length($0))) { L=$0 } END { print L }' datafile

Meaning:

Code:
/M0271857/ # for all lines where your string matches
&& ((!L) || (length(L)>length($0))) # ...and L is undefined or longer than the line
{ L=$0 } # Set L to the entire line
END { print L } # After all lines are processed, print the shortest one found

Hi, thank you for the reply.
i found that if there are two lines which are both the shortest, nothing is returned. is there a way to solve this?
thanks!
# 5  
Old 03-01-2012
Not true for the data I had.

Please post your actual input string and input file. It may be taking some of them to be special characters.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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