Is there a way to preserve color reporting thru awk processing?

Not sure if this is possible, but asking if a "wise one" can enlighten me. :slight_smile:

I use bash.

I have defined the following two aliases:

alias ndate="awk '{ pos=index( \$0, \$9 ) ; rem=substr( \$0, pos ) ; printf(\"%s %3d %10s %10s %10d  %3s %2d %5s  %s\n\", \$1, \$2, \$3, \$4, \$5, \$6, \$7, \$8, rem ) ; } ' "

alias lsh='find . -maxdepth 1 \( ! -type d \) -print | cut -c3- | sort -V | xargs -I here ls -ld --color=auto "here" | ndate '

Unfortunately, the colour from "ls -ld" does not survive the awk post-processing.

Is there an option for awk that would recognize and preserve such colour codes and pass them thru?

1 Like

After digging further afield, it turns out that the issue was the wrong choice of option for the --color switch.

Replacing "auto" by "always" was what I needed to ensure the colors weren't suppressed if passed on to a pipe. (don't I feel stupid! :frowning: )

2 Likes

For those who are interested, here is the full set of special "aliases" that I have for bash. Enjoy! :slight_smile:

These are added to my .bashrc-local file:

######################
###	Define various aliases
######################
echo -e "       alias     ndate = awk '{ pos=index( \$0, \$9 ) ; rem=substr( \$0, pos ) ; printf(\"%s %3d %10s %10s %10d  %3s %2d %5s  %s\n\", \$1, \$2, \$3, \$4, \$5, \$6, \$7, \$8, rem ) ; } ' "
alias ndate="awk '{ pos=index( \$0, \$9 ) ; rem=substr( \$0, pos ) ; printf(\"%s %3d %10s %10s %10d  %3s %2d %5s  %s\n\", \$1, \$2, \$3, \$4, \$5, \$6, \$7, \$8, rem ) ; } ' "


COM='find . -maxdepth 1      -type d    -print | cut -c3- | sort -V | xargs -I here ls -d -1 "here" '
echo -e "       alias     lsdir = ${COM} "
alias lsdir="${COM}"

COM='find . -maxdepth 1      -type d    -print | cut -c3- | sort -V | xargs -I here ls -ld --color=always "here" | ndate '
echo -e "       alias      ldir = ${COM} "
alias ldir="${COM}"

COM='find . -maxdepth 1 \( ! -type d \) -print | cut -c3- | sort -V | xargs -I here ls -d -1 "here" '
echo -e "       alias       lsf = ${COM} "
alias lsf="${COM}"

COM='find . -maxdepth 1 \( ! -type d \) -print | cut -c3- | sort -V | xargs -I here ls -ld --color=always "here" | ndate '
echo -e "       alias        lf = ${COM} "
alias lf="${COM}"

COM='find . -maxdepth 1 \( -name "*.sh" -o -name "*.bash" \) -print | cut -c3- | sort -V | xargs -I here ls -ld --color=always "here" | ndate '
echo -e "       alias        lsh = ${COM} "
eval alias lsh='${COM}'

Just make sure that your .bashrc contains a line

. ~/.bashrc-local
2 Likes