LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2022, 10:07 AM   #1
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 369
Blog Entries: 1

Rep: Reputation: 49
Columnize/align part of the output


Hello Guys,

So I have output coming from a grep that looks like this :

Code:
root@messagerie-principale[10.10.10.19] ~ # cat /tmp/mail.dsn.report.anon
Mar 28 06:25:58 to=<xxx@popcon.debian.org>, relay=pinel.debian.org[209.87.16.63]:25, delay=18, delays=0/0.01/16/1.3, dsn=2.0.0, status=sent (250 OK id=1nYht7-0001L2-VM)
Mar 28 07:00:50 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=32, delays=0/0.01/31/0.68, dsn=2.0.0, status=sent (250 2.0.0 OK  1648447250 l17-20020adf9f11000000b00203e9019608si10192430wrf.908 - gsmtp)
Mar 28 08:00:48 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=32, delays=0.01/0.01/31/0.57, dsn=2.0.0, status=sent (250 2.0.0 OK  1648450848 r6-20020a1c4406000000b0038ce45d579bsi10045041wma.223 - gsmtp)
Mar 28 08:09:25 to=<xxx@mobilis.dz>, relay=mail1.mobilis.dz[196.20.72.23]:25, delay=17, delays=0/0.01/16/1.1, dsn=2.0.0, status=sent (250 2.0.0 22S79NNk030213-22S79NNm030213 Message accepted for delivery)
Mar 28 08:29:30 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.49, dsn=2.0.0, status=sent (250 2.0.0 OK  1648452570 z17-20020adfbbd1000000b0020404325454si10323413wrg.185 - gsmtp)
Mar 28 08:44:05 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=41, delays=0.05/0.01/31/9.7, dsn=2.0.0, status=sent (250 2.0.0 OK  1648453445 g5-20020adfbc85000000b00203e9019361si10789595wrh.229 - gsmtp)
Mar 28 08:53:17 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.3, dsn=2.0.0, status=sent (250 2.0.0 OK  1648453997 v14-20020adfd18e000000b00205932665e7si10387477wrc.772 - gsmtp)
Mar 28 09:00:56 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.58, dsn=2.0.0, status=sent (250 2.0.0 OK  1648454455 n5-20020a1ca405000000b0038c77be9d18si9912077wme.224 - gsmtp)
Mar 28 09:09:32 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=33, delays=0.04/0.01/31/2, dsn=2.0.0, status=sent (250 2.0.0 OK  1648454972 g8-20020adfbc88000000b00205b6c2682dsi6335776wrh.104 - gsmtp)
Mar 28 09:24:43 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=33, delays=0.05/0.01/31/2.2, dsn=2.0.0, status=sent (250 2.0.0 OK  1648455883 f13-20020adf9f4d000000b002041bf4e546si10702625wrg.271 - gsmtp)
root@messagerie-principale[10.10.10.19] ~ #

My wish is to have the output columnized as much as possible. For example, here is an adjusted email "column"

Code:
root@messagerie-principale[10.10.10.19] ~ # some-magic-command
Mar 28 06:25:58 to=<xxx@popcon.debian.org>,      relay=pinel.debian.org[209.87.16.63]:25, delay=18, delays=0/0.01/16/1.3, dsn=2.0.0, status=sent (250 OK id=1nYht7-0001L2-VM)
Mar 28 07:00:50 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=32, delays=0/0.01/31/0.68, dsn=2.0.0, status=sent (250 2.0.0 OK 1648447250 l17-20020adf9f11000000b00203e9019608si10192430wrf.908
- gsmtp)
Mar 28 08:00:48 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=32, delays=0.01/0.01/31/0.57, dsn=2.0.0, status=sent (250 2.0.0 OK 1648450848 r6-20020a1c4406000000b0038ce45d579bsi10045041wma.223
- gsmtp)
Mar 28 08:09:25 to=<xxx@mobilis.dz>,             relay=mail1.mobilis.dz[196.20.72.23]:25, delay=17, delays=0/0.01/16/1.1, dsn=2.0.0, status=sent (250 2.0.0 22S79NNk030213-22S79NNm030213 Message accepted
for delivery)
Mar 28 08:29:30 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.49, dsn=2.0.0, status=sent (250 2.0.0 OK 1648452570 z17-20020adfbbd1000000b0020404325454si10323413wrg.185
- gsmtp)
Mar 28 08:44:05 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=41, delays=0.05/0.01/31/9.7, dsn=2.0.0, status=sent (250 2.0.0 OK 1648453445 g5-20020adfbc85000000b00203e9019361si10789595wrh.229
- gsmtp)
Mar 28 08:53:17 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.3, dsn=2.0.0, status=sent (250 2.0.0 OK 1648453997 v14-20020adfd18e000000b00205932665e7si10387477wrc.772
- gsmtp)
Mar 28 09:00:56 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.58, dsn=2.0.0, status=sent (250 2.0.0 OK 1648454455 n5-20020a1ca405000000b0038c77be9d18si9912077wme.224
- gsmtp)
Mar 28 09:09:32 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=33, delays=0.04/0.01/31/2, dsn=2.0.0, status=sent (250 2.0.0 OK 1648454972 g8-20020adfbc88000000b00205b6c2682dsi6335776wrh.104
- gsmtp)
Mar 28 09:24:43 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=33, delays=0.05/0.01/31/2.2, dsn=2.0.0, status=sent (250 2.0.0 OK 1648455883 f13-20020adf9f4d000000b002041bf4e546si10702625wrg.271
- gsmtp)
root@messagerie-principale[10.10.10.19] ~ #
Where some-magic-command for the moment is

Code:
while read line; do printf "%s %s %s %-32s %s %s %s %s %s %s %s %s %s %s \n" $line; done
(sorry, couldn't do any uglier)

So, the goal is to have at least columns like email, relay, delay, delays, dsn and status aligned.

Is there an easy solution to this or do I have to learn hardcore perl/awk/you name it ?
 
Old 03-31-2022, 10:25 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556
Quote:
Originally Posted by ychaouche View Post
Is there an easy solution to this or do I have to learn hardcore perl/awk/you name it ?
If there is a consistent separator, column can do it, e.g:
Code:
$ echo -e 'a,b,c\n1,2,3' | column -t -s,
a  b  c
1  2  3
If commas are unsafe (i.e. not always a delimiter), then a trivial awk script can likely use FPAT to distinguish those cases and output the fields separated by a suitable delimiter for column to then format.


Last edited by boughtonp; 03-31-2022 at 10:28 AM.
 
1 members found this post helpful.
Old 03-31-2022, 10:31 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by ychaouche View Post
Hello Guys,
So I have output coming from a grep that looks like this :
Code:
root@messagerie-principale[10.10.10.19] ~ # cat /tmp/mail.dsn.report.anon
Mar 28 06:25:58 to=<xxx@popcon.debian.org>, relay=pinel.debian.org[209.87.16.63]:25, delay=18, delays=0/0.01/16/1.3, dsn=2.0.0, status=sent (250 OK id=1nYht7-0001L2-VM)
Mar 28 07:00:50 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=32, delays=0/0.01/31/0.68, dsn=2.0.0, status=sent (250 2.0.0 OK  1648447250 l17-20020adf9f11000000b00203e9019608si10192430wrf.908 - gsmtp)
Mar 28 08:00:48 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=32, delays=0.01/0.01/31/0.57, dsn=2.0.0, status=sent (250 2.0.0 OK  1648450848 r6-20020a1c4406000000b0038ce45d579bsi10045041wma.223 - gsmtp)
Mar 28 08:09:25 to=<xxx@mobilis.dz>, relay=mail1.mobilis.dz[196.20.72.23]:25, delay=17, delays=0/0.01/16/1.1, dsn=2.0.0, status=sent (250 2.0.0 22S79NNk030213-22S79NNm030213 Message accepted for delivery)
Mar 28 08:29:30 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.49, dsn=2.0.0, status=sent (250 2.0.0 OK  1648452570 z17-20020adfbbd1000000b0020404325454si10323413wrg.185 - gsmtp)
Mar 28 08:44:05 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=41, delays=0.05/0.01/31/9.7, dsn=2.0.0, status=sent (250 2.0.0 OK  1648453445 g5-20020adfbc85000000b00203e9019361si10789595wrh.229 - gsmtp)
Mar 28 08:53:17 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.3, dsn=2.0.0, status=sent (250 2.0.0 OK  1648453997 v14-20020adfd18e000000b00205932665e7si10387477wrc.772 - gsmtp)
Mar 28 09:00:56 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.58, dsn=2.0.0, status=sent (250 2.0.0 OK  1648454455 n5-20020a1ca405000000b0038c77be9d18si9912077wme.224 - gsmtp)
Mar 28 09:09:32 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=33, delays=0.04/0.01/31/2, dsn=2.0.0, status=sent (250 2.0.0 OK  1648454972 g8-20020adfbc88000000b00205b6c2682dsi6335776wrh.104 - gsmtp)
Mar 28 09:24:43 to=<xxx@gmail.com>, relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=33, delays=0.05/0.01/31/2.2, dsn=2.0.0, status=sent (250 2.0.0 OK  1648455883 f13-20020adf9f4d000000b002041bf4e546si10702625wrg.271 - gsmtp)
root@messagerie-principale[10.10.10.19] ~ #
My wish is to have the output columnized as much as possible. For example, here is an adjusted email "column"
Code:
root@messagerie-principale[10.10.10.19] ~ # some-magic-command
Mar 28 06:25:58 to=<xxx@popcon.debian.org>,      relay=pinel.debian.org[209.87.16.63]:25, delay=18, delays=0/0.01/16/1.3, dsn=2.0.0, status=sent (250 OK id=1nYht7-0001L2-VM)
Mar 28 07:00:50 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=32, delays=0/0.01/31/0.68, dsn=2.0.0, status=sent (250 2.0.0 OK 1648447250 l17-20020adf9f11000000b00203e9019608si10192430wrf.908
- gsmtp)
Mar 28 08:00:48 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=32, delays=0.01/0.01/31/0.57, dsn=2.0.0, status=sent (250 2.0.0 OK 1648450848 r6-20020a1c4406000000b0038ce45d579bsi10045041wma.223
- gsmtp)
Mar 28 08:09:25 to=<xxx@mobilis.dz>,             relay=mail1.mobilis.dz[196.20.72.23]:25, delay=17, delays=0/0.01/16/1.1, dsn=2.0.0, status=sent (250 2.0.0 22S79NNk030213-22S79NNm030213 Message accepted
for delivery)
Mar 28 08:29:30 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.49, dsn=2.0.0, status=sent (250 2.0.0 OK 1648452570 z17-20020adfbbd1000000b0020404325454si10323413wrg.185
- gsmtp)
Mar 28 08:44:05 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=41, delays=0.05/0.01/31/9.7, dsn=2.0.0, status=sent (250 2.0.0 OK 1648453445 g5-20020adfbc85000000b00203e9019361si10789595wrh.229
- gsmtp)
Mar 28 08:53:17 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.3, dsn=2.0.0, status=sent (250 2.0.0 OK 1648453997 v14-20020adfd18e000000b00205932665e7si10387477wrc.772
- gsmtp)
Mar 28 09:00:56 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=31, delays=0/0.01/31/0.58, dsn=2.0.0, status=sent (250 2.0.0 OK 1648454455 n5-20020a1ca405000000b0038c77be9d18si9912077wme.224
- gsmtp)
Mar 28 09:09:32 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.26]:25, delay=33, delays=0.04/0.01/31/2, dsn=2.0.0, status=sent (250 2.0.0 OK 1648454972 g8-20020adfbc88000000b00205b6c2682dsi6335776wrh.104
- gsmtp)
Mar 28 09:24:43 to=<xxx@gmail.com>,              relay=gmail-smtp-in.l.google.com[74.125.140.27]:25, delay=33, delays=0.05/0.01/31/2.2, dsn=2.0.0, status=sent (250 2.0.0 OK 1648455883 f13-20020adf9f4d000000b002041bf4e546si10702625wrg.271
- gsmtp)
root@messagerie-principale[10.10.10.19] ~ #
Where some-magic-command for the moment is
Code:
while read line; do printf "%s %s %s %-32s %s %s %s %s %s %s %s %s %s %s \n" $line; done
(sorry, couldn't do any uglier)
So, the goal is to have at least columns like email, relay, delay, delays, dsn and status aligned. Is there an easy solution to this or do I have to learn hardcore perl/awk/you name it ?
Very little 'hardcore' about perl/awk, and if you have to do tasks like this often, it should be something you WANT to actually learn, since you'll then have the skills to do it going forward.

To add to what boughtonp said, how do you plan on viewing this? Because if it's just output to a screen, it'll still be hard to read. And going by what you posted, just opening that up in any spreadsheet program (Librecalc, Excel, Google Sheets, etc.), should detect it as a CSV file. And if not, it wouldn't be hard to select it, and it'll 'columnize' it for you. Easier to view/sort/search as well.
 
Old 04-02-2022, 02:10 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
What's wrong with the printf solution? Looks OK to me, except for the unquoted variable, that should be converted to an array first:
Code:
read -a line <<< "$line"
printf ..... "${line[@]}"
 
1 members found this post helpful.
Old 04-02-2022, 03:19 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
How about html or csv format? There are special programs that display those table-like.
 
  


Reply

Tags
alignment, columns, text-fields



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting, getting a counter to align with output? elfoozo Programming 5 08-27-2019 11:24 AM
OpenOffice - How to "columnize" a printout in Spreadsheet? SlowCoder Linux - Software 1 02-12-2008 11:07 AM
align columns of strings and explain '\t' (java) dave bean Programming 3 01-10-2004 10:55 PM
No option to align icons on desktop Dan Cerimele Linux - Newbie 1 11-26-2003 12:10 AM
Red Hat 8, HP842C, Align the print heads? itsjustme Red Hat 0 10-21-2003 03:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration