Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9062243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:41:22+00:00 2026-06-16T15:41:22+00:00

I get some data from a query in mysql that I need to convert

  • 0

I get some data from a query in mysql that I need to convert to a nice looking table.
Then I use awk and printf to align columns, format numbers, etc.

cat /home/jm/mysql/pruebas/pruebat.txt | awk 'BEGIN { FS = ";" } ; {printf "%-'$A's %-'$B's %'\'''$C'd %'\'''$D'd %'\'''$E'd %'\'''$F'.2f %'\'''$F'.2f %'\'''$F'.2f\n", $1, $2, $3, $4, $5, $6, $7, $8 }' >> /home/jm/mysql/pruebas/prn.tx

My problem appears when some fields are too long (say, the % variation between too years is -12.345,67%). I would prefer to show just ###### in that case.
If I use a case/when/then/end in my query, I can substitute the long value by the string ######, but as awk+printf reads this field as a decimal number, it shows 0.00% instead of the desired ######.
Is there any way to write “if this variable is too long, just print ######”?
Thanks,

jm

EDIT:

My source file (forget about headers):

S_V;NO INVENTARIABLES;-41.7000;-67.5200;-25.8200;61.91846523;100.00000000;100.00000000
S_A;ARTICULOS PROMOCIONES;54.4400;631.3200;576.8800;1059.66201323;-1412.30712711;-19.76018501
S_B;PRODUCTOS TECNICOS;975.6000;1951.2000;975.6000;100.00000000;36.01476015;34.61254613
S_S;MATERIAL ELECTRICO;2237.6600;5103.2500;2865.5900;128.06190395;40.21030898;43.42859942

My output:

fam     nombre                               2011       2012        var        %   %mgn11   %mgn12
--------------------------------------------------------------------------------------------------
S_V     NO INVENTARIABLES                     -41        -67        -25    61.92   100.00   100.00
S_A     ARTICULOS PROMOCIONES                  54        631        576 1,059.66 -1,412.31   -19.76
S_B     PRODUCTOS TECNICOS                    975      1,951        975   100.00    36.01    34.61
S_S     MATERIAL ELECTRICO                  2,237      5,103      2,865   128.06    40.21    43.43

My wish (or similar):

fam     nombre                               2011       2012        var        %   %mgn11   %mgn12
--------------------------------------------------------------------------------------------------
S_V     NO INVENTARIABLES                     -41        -67        -25    61.92   100.00   100.00
S_A     ARTICULOS PROMOCIONES                  54        631        576   ######   ######   -19.76
S_B     PRODUCTOS TECNICOS                    975      1,951        975   100.00    36.01    34.61
S_S     MATERIAL ELECTRICO                  2,237      5,103      2,865   128.06    40.21    43.43
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:41:24+00:00Added an answer on June 16, 2026 at 3:41 pm

    Let’s start by rewriting your script in reasonable shell+awk syntax, best I can tell:

    awk -F\; -v fmt="%-${A}s %-${B}s %'${C}d %'${D}d %'${E}d %'${F}.2f %'${F}.2f %'${F}.2f\n" '
    { printf fmt, $1, $2, $3, $4, $5, $6, $7, $8 }
    ' /home/jm/mysql/pruebas/pruebat.txt >> /home/jm/mysql/pruebas/prn.tx
    

    Now, the problem you have is that your format string is specifically saying to print a number when in fact you want to print a string, so use sprintf to create your numbers and then if you’re not happy with the result change it to a string for output. Something like this (untested):

    awk -F\; -v fmt="%-${A}s %-${B}s %'${C}d %'${D}d %'${E}d %'${F}.2f %'${F}.2f %'${F}.2f\n" '
    BEGIN{ split(fmt,fmtA,/ /) }
    {
        for (i=1;i<=NF;i++) {
           $i = sprintf(fmtA[i],$i)
           if (gsub(/[^[:space:]]/,"&",$i) > 6) {
               $i = "######"
           }
        }
        print
    }
    ' /home/jm/mysql/pruebas/pruebat.txt >> /home/jm/mysql/pruebas/prn.tx
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get some data from a mysql database.I'm running a php script
I'm trying to get some data from a table, where the tag has no
I need to get some data from a column. For example I have data
I've a php function to get some data from mysql database: function get_persons() {
I need some help with a MySQL query I'm working on. I have data
I am getting some data from the table in WinForms (MySQL Connector) and would
I have a PHP MySQL query that inserts some data into a MySQL database
Looking for some ideas here... I have a MySQL table that has 100K rows
I need to create an SQL query to insert some data into a table
I get some data from an PHP-Script via AJAX and want to have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.