• Uncategorized

About linux : Linux-shell-CSV-viewer-tool-that-can-freeze-the-header-closed

Question Detail

It’s now hard to write one without the capability of freezing the header, like

function viewcsv() {
   cat $1 | sed -e "s/,,/, ,/g" | column -s, -t | less -#2 -N -S
}

But is there a utility that allows me to freeze the header, or change the one above to allow it?

[EDIT:] by freezing the header I mean when I scroll up and down with “less”, the first line remains there on the top of the screen. Those are usually the header information I want to see even when scrolling down thousands of lines, such as “Date”, “Symbol”, etc.

Question Answer

I adapted this How to scroll large datafile while keeping header to your wishes:

vim -R -u NONE -N +'map <right> 2zl
                    map <left> 2zh
                    map q :qa<CR>
                    se nu sbo=hor scb nowrap
                    1sp
                    winc w
                   ' <(sed -e "s/,,/, ,/g" $1|column -ts,)
  • -R Readonly mode.
  • -u NONE all
    initializations from files and environment variables are
    skipped (they could interfere with the working of e. g. winc w)
  • -N Not compatible mode (otherwise key names like <right> might not work)
  • map <right> 2zl to make scroll horizontally like less -#2
  • map <left> 2zh to make scroll horizontally like less -#2
  • map q :qa<CR> to make Q quit like less
  • se nu sbo=hor scb nowrap set options:
    • ‘number’ – Print the line number in front of each line.
    • ‘scrollopt’=hor – Bind horizontal scrolling for ‘scrollbind’ windows
    • ‘scrollbind’ – to scroll the header line together with the rest of the data
    • ‘nowrap’ – lines will not wrap
      and only part of long lines will be displayed.
  • 1sp Split current window in two (new window has 1 line, the header)
  • winc w move cursor to body window

Off the top of my head, you could tailor the less prompt to display the CSV headers instead of the usual prompt.

viewcsv () {
    less -PM"$(sed -n 's/,/ /gp;q' "$1")" -M -N -S "$1"
}

(You can still see where you are in the file with = — I suppose you could replace the short or medium prompt just as well.)

If your data contains percent signs or question marks, they will need to be escaped, as these have a special meaning in the less prompt definition. See the description of the -P option and the section PROMPTS in the less manual for details.

Maybe you could even throw in some terminal escape sequences to get the prompt (or part of it) displayed at the top of the screen, but the trivial solution would superimpose the prompt over data you want to see.

Are you just asking about not changing the first line? (It’s not at all clear to me what you mean by ‘freezing’, but ‘not modifying’ seems like a reasonable interpretation.)

viewcsv() {
  sed -e "1!s/,,/, ,/g" "$1" | column -s, -t | less -#2 -N -S
}

(Removed the redundant keyword ‘function’ and the UUOC. The relevant addition is the address scope 1! in sed.)

Or perhaps you mean that you want the first line to not go through column:

viewcsv() {
  { sed 1q "$1"; sed -e 1d -e 's/,,/, ,/g' "$1" |
     column -s, -t; } | less -#2 -N -S
}

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.