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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
03-31-2022, 10:07 AM
#1
Member
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 369
Rep:
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 ?
03-31-2022, 10:25 AM
#2
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627
Quote:
Originally Posted by
ychaouche
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.
03-31-2022, 10:31 AM
#3
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753
Quote:
Originally Posted by
ychaouche
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.
04-02-2022, 02:10 AM
#4
LQ Addict
Registered: Dec 2013
Posts: 19,872
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.
04-02-2022, 03:19 AM
#5
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
How about html or csv format? There are special programs that display those table-like.
All times are GMT -5. The time now is 08:10 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News