bash: shortest way to get n-th column of output

后端 未结 8 1808
春和景丽
春和景丽 2020-11-28 03:29

Let\'s say that during your workday you repeatedly encounter the following form of columnized output from some command in bash (in my case from executing svn st

相关标签:
8条回答
  • 2020-11-28 04:15

    To accomplish the same thing as:

    svn st | awk '{print $2}' | xargs rm
    

    using only bash you can use:

    svn st | while read a b; do rm "$b"; done
    

    Granted, it's not shorter, but it's a bit more efficient and it handles whitespace in your filenames correctly.

    0 讨论(0)
  • 2020-11-28 04:15

    Note, that file path does not have to be in second column of svn st output. For example if you modify file, and modify it's property, it will be 3rd column.

    See possible output examples in:

    svn help st
    

    Example output:

     M     wc/bar.c
    A  +   wc/qax.c
    

    I suggest to cut first 8 characters by:

    svn st | cut -c8- | while read FILE; do echo whatever with "$FILE"; done
    

    If you want to be 100% sure, and deal with fancy filenames with white space at the end for example, you need to parse xml output:

    svn st --xml | grep -o 'path=".*"' | sed 's/^path="//; s/"$//'
    

    Of course you may want to use some real XML parser instead of grep/sed.

    0 讨论(0)
提交回复
热议问题