我有一个包含两列的文件列表。我需要删除第一列并保留文件名列表。如果我碰巧有一个以空格结尾的文件,例如"某个文件",那么空格将被awk删除。
文件"输入"的示例(注意"一些文件"}
末尾的空格abc some file
def some other file
运行
cat input | awk '{$1=""; print substr($0, 2)}' > output
将生成文件输出
some file
some other file
其中"某些文件"现在是#34;一些文件"处理文件列表时导致文件不存在。
赞赏任何便携式解决方案:)
[编辑]试图简化上面的例子以使其更清晰,但实际上有更多的列,因此某些解决方案可能不适用。
实际文件是rsync --list-only输出:
drwxr-xr-x 4096 2017/06/04 11:24:21 .
drwxr-xr-x 234234 2017/06/04 11:24:19 some file
drwxr-xr-x 1341212 2017/06/04 11:24:19 some other file
显示文件大小的列可能会展开,因此删除固定数量的尾随字符会导致错误。
文件名确实可以包含路径和多个空格。
示例测试文件(请记住,文件大小可能会有所不同,因此第二列的大小可能会增加):
drwxr-xr-x 4096 2017/06/04 11:24:21 .
drwxr-xr-x 4096 2017/06/04 11:24:19 another
drwxr-xr-x 4096 2017/06/04 11:24:19 another/one
drwxr-xr-x 4096 2017/06/04 11:24:19 another/one/bites
drwxr-xr-x 4096 2017/06/04 11:24:19 another/one/bites/ de_dust
-rw-r--r-- 0 2017/06/04 11:24:19 another/one/bites/ de_dust/ 2017/06/04 11:24:19 Iron Rhapsody
drwxr-xr-x 4096 2017/06/04 11:24:19 phantom of
drwxr-xr-x 4096 2017/06/04 11:24:19 phantom of /the opera
-rw-r--r-- 0 2017/06/04 11:24:19 phantom of /the opera/Bohemian Maiden
[/编辑]
答案 0 :(得分:2)
$ awk '{sub(/[^/]+\/.{15}/,"")}1' file
.
another
another/one
another/one/bites
another/one/bites/ de_dust
another/one/bites/ de_dust/ 2017/06/04 11:24:19 Iron Rhapsody
phantom of
phantom of /the opera
phantom of /the opera/Bohemian Maiden
或使用-E
的GNU或OSX sed(严格按POSIX seds,你可以转义+,{和}):
$ sed -E 's:[^/]+/.{15}::' file
.
another
another/one
another/one/bites
another/one/bites/ de_dust
another/one/bites/ de_dust/ 2017/06/04 11:24:19 Iron Rhapsody
phantom of
phantom of /the opera
phantom of /the opera/Bohemian Maiden
答案 1 :(得分:1)
我建议使用GNU sed:
sed -r 's/^.* [0-9/]{10} [0-9:]{8} //' input
输出:
. some file some other file
答案 2 :(得分:0)
包含tr
和cut
的解决方案:
tr -s ' ' <inputfile | cut -d' ' -f5-