Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

directory list 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

how in easy way rid off from the directory list below those ones which are a already a part of the others on the list?

input:

Code:
/etc
/root
/var/adm
/var/tmp
/var
/home/
/home/test

expected output:

Code:
/etc
/root
/var
/home/

thank you in advance.
 
Hi

If there would be also "/foo/bar" and "/foo/bar/baz", what should appear in the result, "/foo" or "/foo/bar" ?

Are there restrictions on the order of the result ? I mean, is it a problem the order changes during the processing ?


Feherke.
[link feherke.github.com/][/url]
 
This is close...

Code:
sed 's|^/|-|1;s|/.*||g;s|^-|/|1' inputfile | sort | uniq > outputfile
 
Bash:
sed 's/$/\./' /input/file |grep -vf - /input/file

Result:
Code:
/etc
/root
/var
/home/
 


"If there would be also "/foo/bar" and "/foo/bar/baz", what should appear in the result, "/foo" or "/foo/bar" ?"

then /foo/bar if /foo doesn't exist on the list
 


Are there restrictions on the order of the result ? I mean, is it a problem the order changes during the processing ?


and there is no problem to change order in output during the processing

also "/" at the end of line can be rid off

 

I tested your proposals but:

- the bash method doesn't work in my case
- the grep method doesn't work for all cases

Code:
$ cat directory_list
/etc
/root
/var/adm
/var/tmp
/var
/home/
/home/test
/test/1/2/3
/test/1/2/
/test/1/2/3/4
/test/1
$ sed 's|^/|-|1;s|/.*||g;s|^-|/|1' directory_list | sort | uniq
/etc
/home
/root
/test
/var
$


and expected output (thanks to feherke's perceptiveness) is:

Code:
/etc
/root
/var
/home/
/test/1

(can be sorted and of course uniq)
 



and also

/home/

in the output I shown above can be (and even preferred) without trailing "/"

/home
 
Hi

Well, the trailing slash is important in such processing to not consider "/foo/bar" the parent directory of "/foo/bartender". So I explicitly add them everywhere first, then remove them at the end :
Code:
sed '/\/$/!s,$,/,' directory_list | sort | awk '!p||substr($0,1,length(p))!=p{p=$0;sub(/\/$/,"");print}'

Note that the above works supposing the / root directory will never be in the list.

Feherke.
[link feherke.github.com/][/url]
 


hello,

the last Feherke's proposal works fine - only a trailing space can "cheat" a bit.

Code:
$ cat directory_list
/etc
/root
/var/adm
/var/tmp
/var
/home/
/home/test
/test/1/2/3
/test/1/2/
/test/1/2/3/4
/test/1
$ sed '/\/$/!s,$,/,' directory_list | sort | awk '!p||substr($0,1,length(p))!=p{p=$0;sub(/\/$/,"");print}'
/etc
/home/
/home/test
/root
/test/1
/var
$

thank you.
 
Hi

w5000 said:
only a trailing space can "cheat" a bit.
You mean at the end of lines ? Then let us change the Sed code abit :
Code:
sed 's,/* *$,/,' directory_list | sort | awk '!p||substr($0,1,length(p))!=p{p=$0;sub(/\/$/,"");print}'


Feherke.
[link feherke.github.com/][/url]
 
First I was very surprised. Then I realised that your file has hidden spaces at the end of line. So you have to clean it, either directly, or by command (but it becomes long compared too the awk solution)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top