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!
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!
awk '/M0271857/ && ((!L) || (length(L)>length($0))) { L=$0 } END { print L }' datafile
Meaning:
/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,
Try this,
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:-)
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!
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.