Daniel
Daniel

Reputation: 1402

unix console: how to delete lines that contained in other lines

I have a text file that contains sorted paths e.g.

/abc
/abc/def
/abc/jkl
/def
/def/jkl
/def/jkl/yui
/def/xsd
/zde

now I'd like to delete lines that contained in other lines in this case following lines should stay:

/abc/def
/abc/jkl
/def/jkl/yui
/def/xsd
/zde

Upvotes: 1

Views: 20

Answers (1)

falsetru
falsetru

Reputation: 368914

Using awk and tac (concatenate and print files in reverse):

$ tac test.txt | awk '{ if (substr(prev, 1, length($0)) != $0) print $0; prev = $0}' | tac
/abc/def
/abc/jkl
/def/jkl/yui
/def/xsd
/zde

Here's a more readable version of awk:

{
    if (substr(prev, 1, length($0)) != $0)  # Compare with last line (substring?)
        print $0;
    prev = $0  # Remember the last line
}

Upvotes: 1

Related Questions