Linux shell CSV viewer that can freeze the header?

Now it is difficult to write one without the option to freeze the title 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 it above to allow this?

[EDIT:] by freezing the title, I mean when I scroll up and down with "less", the first line stays there at the top of the screen. This is usually the header information that I want to see even when scrolling through thousands of lines like Date, Symbol, etc.

+3


source to share


3 answers


I adapted this one how to loop through a large data file keeping the header as you wish:

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 initialization from files and environment variables (they can interfere with work, for example winc w

    )
  • -N

    Incompatible mode (otherwise key names such as <right>

    may not work)
  • map <right> 2zl

    to make it scroll horizontally likeless -#2

  • map <left> 2zh

    to make it scroll horizontally likeless -#2

  • map q :qa<CR>

    to make it Qcomplete likeless

  • se nu sbo=hor scb nowrap

    set parameters:
    • 'number' - print the line number before each line.
    • 'scrollopt' = hor - bind horizontal scrolling to 'scrollbind' windows
    • 'scrollbind' - scrolls the title bar along with the rest of the data
    • 'nowrap' - lines will not be wrapped and only part of long lines will be displayed.
  • 1sp

    Split the current window into two (new window has 1 row, title)
  • winc w

    move cursor to body window
+2


source


Are you only asking not to change the first line? (This is not at all what you mean by "freezing", but "not changing" seems like a reasonable interpretation.)

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

      

(Removed redundant "function" keyword and UUOC. Corresponding addition is address scope 1!

c sed

.)



Or maybe you want the first line not to go through column

:

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

      

0


source


Off the top of my head, you can customize the prompt less

to display CSV headers instead of a regular prompt.

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

      

(You can still see where you are in the c file =

- I suppose you could replace the short or medium prompt as well.)

If your data contains percent signs or question marks, they must be escaped as they have special meaning in defining the prompt less

. For details, see the Description of the option -P

and the PROMPTS section in the manual less

.

Perhaps you can even add some terminal escape sequences to get the hint (or part of it) displayed at the top of the screen, but a trivial solution imposes a query on the data you want to see.

0


source







All Articles