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

后端 未结 8 1958
春和景丽
春和景丽 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:06

    Try the zsh. It supports suffix alias, so you can define X in your .zshrc to be

    alias -g X="| cut -d' ' -f2"
    

    then you can do:

    cat file X
    

    You can take it one step further and define it for the nth column:

    alias -g X2="| cut -d' ' -f2"
    alias -g X1="| cut -d' ' -f1"
    alias -g X3="| cut -d' ' -f3"
    

    which will output the nth column of file "file". You can do this for grep output or less output, too. This is very handy and a killer feature of the zsh.

    You can go one step further and define D to be:

    alias -g D="|xargs rm"
    

    Now you can type:

    cat file X1 D
    

    to delete all files mentioned in the first column of file "file".

    If you know the bash, the zsh is not much of a change except for some new features.

    HTH Chris

提交回复
热议问题