Tiny Core Linux
General TC => Programming & Scripting - Unofficial => Topic started by: deetee on May 16, 2013, 10:27:50 AM
-
Hi all!
I would like to start an awk-printf at a definite column.
So far I use
printf "\t\t\t\t\t%s",$1;
to start printing at column 40 (5 tabs รก 8 spaces).
It works, but there should be a better solution which overcomes the uncertainty of exactly 8 spaces per tab or to start i.e. at column 42.
Printing SPACE-characters doesn't make sense for my requirements because there still is some data at column 1 to 39 - which will be overwritten (tabs are working properly in this case).
TIA
deetee
-
awk supports print format conversions as in C therefore formating reports should not be an issue.
So you have available %s, %c, %e, %f, %g ... with width and precision parameters between the % and the control character.
Google to get many examples.
-
Am always inspired to learn awk especially when I see what robert's can accomplish with it. I have yet to take advantage of awk's strengths.
When printing a single line to the terminal can we not use ASCII escape sequences, hopefully nothing considered wrong with that..?
printf "\033[12;40H\033[Khello Core \n"
I understand this may be terminal dependent and possibly needs bash, but works reliably in Core (with bash.tcz). The sequence will move the cursor to line 12 column 40, then clear to the end of that line from column 40, then print hello Core at column 40, then move the cursor to a new line.
-
Thanks for the hints.
@ roberts:
I don't know exactly if you are the famous founder of TC - but it's a great honor for me to learn from you.
If I understand you right I should modify %s to i.e. %42s.
That works in principle but "overwrites" columns 1 to 41 with blanks (the tabs didn't do that).
@ coreplayer2:
I'm still working with escape sequences (which work properly with busybox/ash and the TC-terminal). But in this case I would like to print a ls-command ("formatted" with awk/printf) which generates an output of many lines. And every line should start at the defined column. The whole command looks in principle (ls is much more bloated and numbers are variables) like this:
CSI=$'\x1b['
echo -n "${CSI}1;41H"
ls -lAh | awk '{ printf "\t\t\t\t\t %s %-28s %6s\n",substr($1,1,1),substr($9,1,28),$5;}' | head -5 | tail -3
The sense of this code is to print the right panel (line 3 to 5) of a file manager (similar to midnight commander). And the requirement is to not overwrite the left panel.
Do you have any other ideas to improve my 5-tabs-solution?
TIA
deetee
-
Hi deetee
If you are trying to maintain a single screen full of information, moving the cursor to desired position prior to printing
might be the simplest solution.
-
And if you're doing terminal output with that level of precision, using ncurses and C/C++ would be much more appropriate than scripts.
(though you can move the cursor in a terminal-agnostic way using tput, an util to ncurses, total control seems it would be better)
-
As an alternative to ncurses you can use slang too.
-
Thank you all for your hints and suggestions.
I considered to write my "application" with c++/n-curses, but unfortunately I had the crazy idea to write a file manager (inspired by mc) as shell script (small; easy to maintain; no dependencies; close to the operating system; customized to TC/busybox/ash).
After some hours of programming I got a working version (size: 12k) which I want to present to this forum soon (it still needs a little bit more stability).
Regards
deetee
-
That's interesting bmarcus. Any ideas on how slang compare to ncurses ? in terms of precision and extension size ?
Thx