Tiny Core Linux

General TC => Programming & Scripting - Unofficial => Topic started by: deetee on May 16, 2013, 07:27:50 AM

Title: script/awk/printf - How to printf string starting at a definite column?
Post by: deetee on May 16, 2013, 07:27:50 AM
Hi all!

I would like to start an awk-printf at a definite column.

So far I use
Code: (bash) [Select]
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
Title: Re: script/awk/printf - How to printf string starting at a definite column?
Post by: roberts on May 16, 2013, 08:17:30 AM
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.
Title: Re: script/awk/printf - How to printf string starting at a definite column?
Post by: coreplayer2 on May 16, 2013, 03:05:38 PM
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.

Title: Re: script/awk/printf - How to printf string starting at a definite column?
Post by: deetee on May 16, 2013, 10:57:47 PM
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:
Code: (ash) [Select]
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
Title: Re: script/awk/printf - How to printf string starting at a definite column?
Post by: Rich on May 16, 2013, 11:18:12 PM
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.
Title: Re: script/awk/printf - How to printf string starting at a definite column?
Post by: curaga on May 17, 2013, 03:31:28 AM
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)
Title: Re: script/awk/printf - How to printf string starting at a definite column?
Post by: bmarkus on May 17, 2013, 03:48:43 AM
As an alternative to ncurses you can use slang too.
Title: Re: script/awk/printf - How to printf string starting at a definite column?
Post by: deetee on May 17, 2013, 04:37:47 AM
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
Title: script/awk/printf - How to printf string starting at a definite column?
Post by: coreplayer2 on May 17, 2013, 03:31:44 PM
That's interesting bmarcus.  Any ideas on how slang compare to ncurses ? in terms of precision and extension size ?

Thx