Bash/shell 使第二列变为红色

Bash/shell 使第二列变为红色

~/.bashrc我最近升级了以下功能:

dircat() {
    if [[ $# -eq 0 ]] ; then
        echo "Directory tree required, eg: dircat /sys/class/drm/*/status"
        echo "          or: dircat /sys/devices/system/cpu/intel_pstate/*"
        echo "          or: dircat /sys/devices/system/cpu/cpufreq/policy0/*"
        echo "NOTE: Only use 'dircat' on directories containing one line files."
        return 1
    fi

    prefix="$1"                         # Initial match is file 1's name
    len="${#1}"                         # Max compare length is file 1's name
    for file in "$@" ; do               # For every filename in '*' glob
        for ((i=0; i<len; i++)); do     # Find longest common prefix
            [[ "${prefix:i:1}" == "${file:i:1}" ]] && continue   # matching
            prefix="${prefix:0:i}"      # All the matching characters
            prefix="${prefix%\/*}/"     # Characters up to last "/"
            len="${#prefix}"            # New compare length for next filename
            break
        done
    done

    paste <(ls "$@") <(cat "$@") | column -s $'\t' -t | sed "s#$prefix##g"

} # dircat ()

我想通过将第二列(或者最后一列)变为红色来进一步增强它:

dircat 输出.png

答案1

使用bac0n 的我替换的评论:

paste <(ls "$@") <(cat "$@") | column -s $'\t' -t | sed "s#$prefix##g"

和:

paste <(ls "$@") <(printf '\e[1;91m%s\e[0m\n' $(cat "$@")) | \
    column -s $'\t' -t | sed "s#$prefix##g"

现在它按期望显示:

dircat 输出 2.png

相关内容