find directory | sort -u |sed 'N;/^/(./+/)/n/1/!P;D' 获取directory目录下所有文件的全路径
find 获取路径,但是包括目录
sort -u 排序
sed 'N;/^/(./+/)/n/1/!P;D' 排除目录的路径
N 读入下一行至模式空间
/^/(./+/)/n/1/ 匹配模式空间中,第一个换行(/n)符号前与后面有重复的,也就是匹配第一个换行符前的字符在后面是否再次出现,如:
/home/user /n /home/user/date
/1 /n
!P 如果匹配成功,不打印,否则打印
D 删除模式空间开头到第一个/n(含)之间的内容,即上面红色部分
再贴上sed 的N P D的解释:
P: 就是输出模式空间开头到第一个/n之间的内容
D: If pattern space contains no newline, start a normal new cycle as if the d command was issued. Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the resultant pattern space, without reading a new line of input.
(是删除模式空间开头到第一个/n(含)之间的内容,并且控制流跳到脚本的第一条语句。这里一定要注意这句话“and restart cycle with the resultant pattern space, without reading a new line of input.”,即它是在不改变当前行号的情况下,从头执行的。)
这句话也充分的说明了,为什么很多人不愿意读中文翻译版书籍的原因。:-)
N: Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then sed exits without processing any more commands. (追加下一个输入行到模式空间后面并在二者间嵌入一个新行,改变当前行号码。如果没有下一个可处理的行,则退出)
注意大小写n d的区别