Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Solved] Bash align text with printf

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Allain
Posts: 20
Joined: 2016-12-06 13:55

[Solved] Bash align text with printf

#1 Post by Allain »

Good evening

I have tried how to align text correctly
I can't get better than this
What can i do ?

The text file i am using :
  • PK 744 - Hide Activities Button - User - Enabled -
    PK 72 - Recent Items - User - Disabled -
    PK 584 - TaskBar - User - Enabled -
    PK 704 - All Windows - User - Disabled -
    PK 15 - AlternateTab - System - Enabled - Favorite
    PK 6 - Applications Menu - System - Disabled -
    PK 16 - Auto Move Windows - System - Disabled -
    PK 779 - Clipboard Indicator - User - Enabled - Favorite
    PK 307 - Dash to Dock - User - Enabled - Favorite
    PK 884 - Disper Menu - User - Disabled -
    PK 7 - Removable Drive Menu - System - Disabled -
    PK 442 - Drop Down Terminal - User - Enabled -
    PK 885 - Dynamic Top Bar - User - Enabled - Favorite
    PK 810 - Hide App Icon - User - Enabled -
    PK 544 - HistoryManager Prefix Search - User - Enabled -
    PK 600 - Launch new instance - System - Disabled -
    PK 36 - Lock Keys - User - Enabled -
    PK 898 - MMOD Panel - User - Disabled -
    PK 18 - Native Window Placement - System - Disabled -
    PK 750 - OpenWeather - System - Enabled - Favorite
    PK 708 - Panel OSD - User - Enabled -
    PK 8 - Places Status Indicator - System - Disabled -
    PK 836 - Internet Radio - User - Disabled -
    PK 881 - Screenshot Window Sizer - System - Disabled -
    PK 9 - SystemMonitor - System - Disabled -
    PK 1052 - Taskwarrior Integration - User - Disabled -
    PK 570 - Todo.txt - User - Enabled - Favorite
    PK 1166 - Extension Update Notifier - User - Enabled -
    PK 19 - User Themes - System - Disabled -
    PK 549 - Web Search Dialog - User - Enabled -
    PK 602 - Window List - System - Disabled -
    PK 10 - windowNavigator - System - Disabled - Favorite
    PK 21 - Workspace Indicator - System - Disabled - Favorite
My script :

Code: Select all

#!/bin/bash

      input="status.fav"
      ni=0
      while IFS= read -r var
      do
        var=`sed 's/ - /-/g' <<< $var`
        IFS="-" read -a arr <<< $var
        #echo ${#arr[@]}
        IFS=" " read -a arr2 <<< ${arr[0]}
        #stri=${arr[0]}
        strpk1=${arr2[0]}
        strpk2=${arr2[1]}
        strname=${arr[1]}
        stred=${arr[2]}
        strusrsys=${arr[3]}
        strfav=${arr[4]}
        #echo $stri1 ; echo $stri2 ; echo $strj ; echo $strk ; echo $strl ; echo $strm ; echo "---"
        line='..........................................................................................'
        format="%-2s %4d %-80s %s %-10s %-10s %-10s\n"
        printf "$format" "$strpk1" "$strpk2" "$strname ${line:${#strname}}" "$stred" "$strusrsys" "$strfav"
        ni=$[ni+1]
        nf=`grep Favorite status | wc -l`
        ne=`grep Enabled status | wc -l`
        nd=`grep Disabled status | wc -l`
        nu=`grep User status | wc -l`
        ns=`grep System status | wc -l`
      done < "$input"
      echo "(Extensions installed : $ni - Favorites : $nf - Enabled : $ne - Disabled : $nd - User : $nu - System : $ns)"
Also printf is slower than echo
Last edited by Allain on 2017-07-03 08:14, edited 1 time in total.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Bash align text with printf

#2 Post by pylkko »

maybe use column?

see:

Code: Select all

man column
I think it is installed by default on Debian (it is in package bsdmainutils)

But I am not sure I understand what you want.

Allain
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash align text with printf

#3 Post by Allain »

The output would like this
I tried with 'column' which seems fine but my output is too long
  • column: line too long
    column: line too long
    column: line too long
    ...
My output, it is not well formatted on the end of the line
Numbers are aligned on right side
  • PK 744 Hide Activities Button .................................................................... User Enabled
    PK 72 Recent Items .............................................................................. User Enabled
    PK 584 TaskBar ................................................................................... User Enabled
    PK 704 All Windows ............................................................................... User Disabled
    PK 15 AlternateTab .............................................................................. System Enabled Favorite
    PK 6 Applications Menu ......................................................................... System Disabled
    PK 16 Auto Move Windows ......................................................................... System Disabled
    PK 779 Clipboard Indicator ....................................................................... User Enabled Favorite
    PK 307 Dash to Dock .............................................................................. User Enabled Favorite
    PK 884 Disper Menu ............................................................................... User Disabled
    PK 7 Removable Drive Menu ...................................................................... System Disabled
    PK 442 Drop Down Terminal ........................................................................ User Enabled
    PK 885 Dynamic Top Bar ........................................................................... User Enabled Favorite
    PK 810 Hide App Icon ............................................................................. User Enabled
    PK 544 HistoryManager Prefix Search .............................................................. User Enabled
    PK 600 Launch new instance ....................................................................... System Disabled
    PK 36 Lock Keys ................................................................................. User Enabled
    PK 898 MMOD Panel ................................................................................ User Disabled
    PK 18 Native Window Placement ................................................................... System Disabled
    PK 750 OpenWeather ............................................................................... System Disabled Favorite
    PK 708 Panel OSD ................................................................................. User Enabled
    PK 8 Places Status Indicator ................................................................... System Disabled
    PK 836 Internet Radio ............................................................................ User Disabled
    PK 881 Screenshot Window Sizer ................................................................... System Disabled
    PK 9 SystemMonitor ............................................................................. System Disabled
    PK 1052 Taskwarrior Integration ................................................................... User Disabled
    PK 570 Todo.txt .................................................................................. User Enabled Favorite
Take note than in my input file when the hyphen finish the line, it is followed by a blank space
because the blank space can be replaced by 'Favorite'

Note :
I found the command 'pr' but i get no luck with it now
pr -s'-' -6 -e'-' -t | awk '{printf "%-3s %-5s %-80s %-10s %-10s %-10s\n", $strpk1, $strpk2, $strname, $stred, $strusrsys, $strfav}'
column is ok with files, not with variables
and can't enlarge the name field with dots
Last edited by Allain on 2017-06-27 12:45, edited 1 time in total.

kmathern
Posts: 603
Joined: 2011-02-05 19:20

Re: Bash align text with printf

#4 Post by kmathern »

Allain wrote:The output would like this
I tried with 'column' which seems fine but my output is too long ...
Try adding the column command's -t and -s options

Code: Select all

$ column -t -s "-" example
    PK 744     Hide Activities Button          User      Enabled 
    PK 72      Recent Items                    User      Disabled 
    PK 584     TaskBar                         User      Enabled 
    PK 704     All Windows                     User      Disabled 
    PK 15      AlternateTab                    System    Enabled     Favorite
    PK 6       Applications Menu               System    Disabled 
    PK 16      Auto Move Windows               System    Disabled 
    PK 779     Clipboard Indicator             User      Enabled     Favorite
    PK 307     Dash to Dock                    User      Enabled     Favorite
    PK 884     Disper Menu                     User      Disabled 
    PK 7       Removable Drive Menu            System    Disabled 
    PK 442     Drop Down Terminal              User      Enabled 
    PK 885     Dynamic Top Bar                 User      Enabled     Favorite
    PK 810     Hide App Icon                   User      Enabled 
    PK 544     HistoryManager Prefix Search    User      Enabled 
    PK 600     Launch new instance             System    Disabled 
    PK 36      Lock Keys                       User      Enabled 
    PK 898     MMOD Panel                      User      Disabled 
    PK 18      Native Window Placement         System    Disabled 
    PK 750     OpenWeather                     System    Enabled     Favorite
    PK 708     Panel OSD                       User      Enabled 
    PK 8       Places Status Indicator         System    Disabled 
    PK 836     Internet Radio                  User      Disabled 
    PK 881     Screenshot Window Sizer         System    Disabled 
    PK 9       SystemMonitor                   System    Disabled 
    PK 1052    Taskwarrior Integration         User      Disabled 
    PK 570     Todo.txt                        User      Enabled     Favorite
    PK 1166    Extension Update Notifier       User      Enabled 
    PK 19      User Themes                     System    Disabled 
    PK 549     Web Search Dialog               User      Enabled 
    PK 602     Window List                     System    Disabled 
    PK 10      windowNavigator                 System    Disabled    Favorite
    PK 21      Workspace Indicator             System    Disabled    Favorite

Allain
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash align text with printf

#5 Post by Allain »

Column displays a well aligned text but it fills the half of the screen

printf allows me to work with variables on each line
i find this solution better

but to align the end of each line is is not the easy part

Allain
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash align text with printf

#6 Post by Allain »

I have improved my script and added a column
Changed printf settings and it is near perfect
Except the last column
https://ibb.co/eCmmCk

Code: Select all

        format="\t%2s %5i %-75s %-10s %-8s %-10s %-10s\n"
        printf "$format" "$strpk1" "$strpk2" "$strname" "$stred" "$strusrsys" "$strfav" "$strsett "
I get it aligned if i remove the favorites
https://ibb.co/io7F55

Maybe i go with awk and change completely my function
so i can play with reverse video for the text or other settings

awk makes it perfect but the result is a text file, not variables to play with

Code: Select all

cat statusawk | awk -F "-" '{ printf("\t%- 2s % 5i  %- 75s  %- 10s %- 10s %- 10s %- 10s\n", $1, $2, $3, $4, $5, $6, $7, $8); }'
https://ibb.co/k4wsk5

i think i can do that :-)
I got new ideas

Allain
Posts: 20
Joined: 2016-12-06 13:55

Re: Bash align text with printf

#7 Post by Allain »

Solution with printf
It is misaligned because the variable is empty
I just fill it with one blank space and it rocks
But it is always slow, displaying one line by one

Post Reply