unix console: how to delete lines that contained in other lines -
i have text file contains sorted paths e.g.
/abc /abc/def /abc/jkl /def /def/jkl /def/jkl/yui /def/xsd /zde
now i'd delete lines contained in other lines in case following lines should stay:
/abc/def /abc/jkl /def/jkl/yui /def/xsd /zde
using awk
, tac
(concatenate , 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 more readable version of awk:
{ if (substr(prev, 1, length($0)) != $0) # compare last line (substring?) print $0; prev = $0 # remember last line }
Comments
Post a Comment