【问题标题】:Make awk keep ending whitespace让 awk 不断结束空格
【发布时间】:2017-06-04 21:08:05
【问题描述】:

我有一个包含两列的文件列表。我需要删除第一列并保留文件名列表。如果我碰巧有一个以空格结尾的文件,例如“某个文件”,则空格会被 awk 删除。

以文件“输入”为例(注意“某些文件”末尾的空格)

abc some file 
def some other file

跑步

cat input | awk '{$1=""; print substr($0, 2)}' > output

将产生文件输出

some file
some other file

“some file”现在是“some file”,导致处理文件列表时文件不存在。

任何便携式解决方案都值得赞赏:)

[编辑] 试图简化上面的示例以使其更清晰,但实际上列更多,因此某些解决方案可能不适用。

实际文件是 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

[/编辑]

【问题讨论】:

    标签: bash shell awk rsync


    【解决方案1】:
    $ 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
    

    或使用 GNU 或 OSX sed 获取 -E(使用严格的 POSIX sed,您可以转义 +、{ 和 }):

    $ 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
    

    【讨论】:

    • 由于文件大小不同,文件名之前并不总是有 44 个字符。文件名可能包含路径,因此“/”可以出现在文件名中。试图阅读你的正则表达式,请解释最后一个?仍然需要适应我的要求:)
    • 谢谢,完成了编辑:) 不擅长正则表达式,有没有办法说remove from the start of line to hte 17 characters after the first / on the line 可能吗?使用通配符总是会捕捉到最后一个,我不知道如何获得第一个。谢谢。
    • 你的答案太快了,我刚刚有时间将你的最后一个改进为sed -E 's/^.{10} +[0-9]+ [0-9/]{10} [0-9:]{8} //',我猜这应该是防弹的。
    • 为我工作。非常感谢:)
    • 不客气。现在删除我的 cmets 进行整理。
    【解决方案2】:

    我建议使用 GNU sed:

    sed -r 's/^.* [0-9/]{10} [0-9:]{8} //' input
    

    输出:

    . 一些文件 其他一些文件

    【讨论】:

    • 看起来不错,请解释一下正则表达式中的 {10} 和 {8} 吗?
    • 精确匹配 0,1,2,3,4,5,6,7,8,9 和 / 的 10 个(连续)字符。
    • 谢谢,刚刚在 Linux 和 BSD 上测试过,似乎很好用 :)
    • @EdMorton:好的,我建议使用:sed -r 's/^.{10} [0-9 ]+ [0-9/]{10} [0-9:]{8} //'
    【解决方案3】:

    trcut 的解决方案:

     tr -s ' ' <inputfile | cut -d' ' -f5-
    

    【讨论】:

    • 只有在没有超过一个连续空格的文件名的情况下才能工作
    • @deajan 它也会去掉前导空格,但您的要求中没有说明。
    猜你喜欢
    • 2013-11-25
    • 2020-04-25
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多