WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: script/awk/printf - How to printf string starting at a definite column?  (Read 5737 times)

Offline deetee

  • Newbie
  • *
  • Posts: 34
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

Offline roberts

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 7361
  • Founder Emeritus
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.
10+ Years Contributing to Linux Open Source Projects.

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
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.

« Last Edit: May 17, 2013, 12:03:06 AM by coreplayer2 »

Offline deetee

  • Newbie
  • *
  • Posts: 34
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
« Last Edit: May 17, 2013, 02:52:57 AM by deetee »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11618
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.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11044
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)
« Last Edit: May 17, 2013, 06:33:26 AM by curaga »
The only barriers that can stop you are the ones you create yourself.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
As an alternative to ncurses you can use slang too.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline deetee

  • Newbie
  • *
  • Posts: 34
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
« Last Edit: May 17, 2013, 07:40:54 AM by deetee »

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
That's interesting bmarcus.  Any ideas on how slang compare to ncurses ? in terms of precision and extension size ?

Thx
« Last Edit: May 17, 2013, 06:48:55 PM by coreplayer2 »