On Fri, Dec 11, 2015 at 08:41:26PM -0700, valkrem wrote:
> Assume I have a file named  "test"  and has three variables
> 
> Id     Date      length
> 
> 123 20150518  2750 
> 125 20140324  3500
> 
> When I invoke  the command -ruler ( or  the script name)  I will see the 
> following
> 
> bash$ ruler test
> 1                        2         3
> 12345678901234567890
> -----------------------------------
> 123 20150518  2750 
> 125 20140324  3500

Your lines don't align, so I took the liberty of formatting the output
the way I saw fit.  Here's an alternative to Stephane's solution:

ruler() {
    local cols=${COLUMNS:-$(tput cols)}
    local i n tmp
    if (( ${#_ruler_line1} < cols )); then
        n=$(( ${#_ruler_line1} / 10 ))
        for ((i=n+1; i*10 <= cols; i++)); do
            printf -v tmp %10d "$i"
            _ruler_line1+=$tmp
        done
    fi
    while (( ${#_ruler_line2} < cols )); do
        _ruler_line2+=1234567890
    done
    echo "${_ruler_line1:0:cols}"
    echo "${_ruler_line2:0:cols}"
    cat "$@"
}

It could be done as a script instead of a function, but the function
version keeps _ruler_line1 and _ruler_line2 around as global variables
so it doesn't have to recreate them every time.  It simply appends to
them whenever the terminal width increases.

Reply via email to