How to select the shortest path in grep search?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to select the shortest path in grep search?
# 1  
Old 06-09-2012
How to select the shortest path in grep search?

Hi,

How can I display only one shortest path (A/B/configure)?

Code:
$ grep configure file.txt

A/B/configure
A/B/C/configure
A/B/C/D/configure

Thank you.

Last edited by Scrutinizer; 06-10-2012 at 05:39 PM.. Reason: code tags
# 2  
Old 06-10-2012
Code:
perl -lne 'BEGIN { $c = 0 }
if (/configure/) {
    $t = s:/:/:g;
    if ($c != 0 && $t < $c) { $c = $t; $s = $_ }
    elsif ($c == 0) { $c = $t; $s = $_ }
}
END { print "$s" }' file.txt

# 3  
Old 06-10-2012
Try:
Code:
awk -F/ 'NR==1 || NF<m{m=NF; p=$0} END{print p}' infile

or
Code:
awk -F/ 'NR==1 || length<m{m=length; p=$0}END{print p}' infile

depending on what you mean with shortest path Smilie

--edit--
Actually we should count the sequences of non-slashes to find the least deepest path, so that we do not count leading, ending and double slashes ( leaving paths with ../ out of the equation Smilie )
Code:
awk 'NR==1 || gsub(/[^\/]+/,"&")<m{m=NF; p=$0} END{print p}' infile


Last edited by Scrutinizer; 06-10-2012 at 04:00 AM..
# 4  
Old 06-10-2012
Quote:
Originally Posted by Scrutinizer
Try:
.......
depending on what you mean with shortest path Smilie
Thanks Scrutinizer and other responses, that is exactly what I am looking for, you are the best :-).


Cheers.
# 5  
Old 06-10-2012
Quote:
Originally Posted by Scrutinizer
Actually we should count the sequences of non-slashes to find the least deepest path, so that we do not count leading, ending and double slashes ( leaving paths with ../ out of the equation Smilie )
Code:
awk 'NR==1 || gsub(/[^\/]+/,"&")<m{m=NF; p=$0} END{print p}' infile

That solution doesn't handle consecutive slashes properly. Specifically, m=NF renders the comparison of gsub's return value meaningless.

Feeding the following pathological data should demonstrate the problem:
Code:
A/B/C/configure
A/////B/configure

I'll leave the correction as an exercise for someone else, but I believe that using this approach requires storing the return value of gsub for subsequent comparison.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 06-10-2012
Ow, yes.. too hasty a modification of the previous command... This better?
Code:
awk '(n=gsub(/[^\/]+/,"&"))<m || NR==1{m=n;p=$0} END{print p}' infile

# 7  
Old 06-10-2012
Quote:
Originally Posted by Scrutinizer
Ow, yes.. too hasty a modification of the previous command... This better?
Code:
awk '(n=gsub(/[^\/]+/,"&"))<m || NR==1{m=n;p=$0} END{print p}' infile

Looks good to me.

Here's a slightly more succinct version which should be equivalent (only very lightly tested):
Code:
awk -F./+. 'NR==1 || NF<m {m=NF;p=$0} END{print p}' infile

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Shortest path for each query from a csv file

Hi all, I have this file __DATA__ child, Parent, probability, M7, Q, P, M7, M28, E, M28, M6, E, M6, Q, Pl, & several hundred lines..... Legends: P(= Probable) > Pl(=Plausible) > E(=Equivocal). What I want is for each child I want to trace it... (5 Replies)
Discussion started by: rushadrena
5 Replies

3. Shell Programming and Scripting

Select only line numbers using grep

Hai, I want to select only line numbers into a file if some pattern matches. I have written my script like below but its not working. #!/bin/sh file='/home/testfile1' filesearch='/home/test00' while read line do search=`echo $line |cut -c 1-24` echo $search echo `grep -n ""... (3 Replies)
Discussion started by: Subbu123
3 Replies

4. Shell Programming and Scripting

How to select First two directory in from path name?

Can somebody help me on below question I have path and i want to select first two directory name in variable how we can do this. /Myname/xyz/yourname/abc/somebodyname i want to select /Myname/xyz in variable. Quick help will be appriciated. Thanks in advance Regards, Kumar (10 Replies)
Discussion started by: yadavricky
10 Replies

5. UNIX for Advanced & Expert Users

SQLPlus Select with Grep

I m trying to grep 'select * from' from a script, but the problem is with the * ... I think its taking * as some special character ... when I try to do grep '*' test (test is my test file) its displaying * But when I am trying to do grep 'select * from' test its not showing anything... (2 Replies)
Discussion started by: RDR
2 Replies

6. Shell Programming and Scripting

search & select text

hi people; from my file:... ... ... 101221-18:45:17 192.168.1.1 1.0 PortNodeModel ========================================================= Object Attribute Value ========================================================= SectorPort=3 switchport 20 SectorPort=1 switchport 10 SectorPort=2... (12 Replies)
Discussion started by: gc_sw
12 Replies

7. Programming

Implementing a shortest path algorithm in C

Hello, I have a question. I have to implement a shortest path algorithm in n*n grid, moving from one coordinate to another coordinate. But i have no clue how to start. Can anyone help? Thanks a tonn in advance! (1 Reply)
Discussion started by: mind@work
1 Replies

8. Shell Programming and Scripting

regex to select last part of a path

Hi all, I am learning the use of regular expression and I would like to know which regex can be used to select only the last part of a directory path name. Something like: /dir1/dir2/dir2 and I want to select the last /dir2 where dir2 can be any kind of string. Thanks a lot for your help.... (7 Replies)
Discussion started by: elric
7 Replies

9. Shell Programming and Scripting

How to select the path that contains a certain string from a certain file?

Hi, I am new to this world of shell programming. I am facing a problem that is : I have directory which has many sub directories at different depth. say A/B/C/files A/B/files A/B/C/D/files In this directory structure there exists a file called ".project" in some of the sub... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

10. UNIX for Dummies Questions & Answers

Select records based on search criteria on first column

Hi All, I need to select only those records having a non zero record in the first column of a comma delimited file. Suppose my input file is having data like: "0","01/08/2005 07:11:15",1,1,"Created",,"01/08/2005" "0","01/08/2005 07:12:40",1,1,"Created",,"01/08/2005"... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question