You are not logged in.

#1 2012-01-05 15:35:07

Fackamato
Member
Registered: 2006-03-31
Posts: 579

[solved] Text formatting in bash

Hoi lads,

I've a script that generally checks a host's health. The output might be (partly) something like this:

x.laptop.x.com
 15:31:12 up  4:34,  4 users,  load average: 1.31, 1.30, 1.27

===> Checking DMI:
OK: Dell/Lenovo S/N found, x
OK: Asset tag, x

!!=> ipmitool not found, unable to check BMC/console.

!!=> ipmitool not found, unable to check sensor data.

===> Checking SATA HDDs with smartctl:
Port		S/N / FW		Size	Pending sct Reall_sct Uncor_sct Offline_sct End2end CRC MZONE RAW SMART
/dev/sda	x	320 GB	0		0		0		0		n/a	0	OK
/dev/sdb	OCZ-x	60 GB	0		0		0		0		n/a	0	OK
INFO: 2 HDDs found

===> Checking NICs with magic:
WARN: eth0 up@100, 0 collisions, 0 dropped RX, 0 dropped TX, 0 RX err, 0 TX err
x@x:~/dev/checkhost$ 

Look at checking SATA HDDs, I used to have tabs separate the headers and all values, but it gets way too wide. I can stick with having Port, S/N & Size separated by tabs, but the rest of the values are either 0 (1 character) or something up to 4 characters (like 2047 reallocated sectors, yikes).
It looks ugly if I separate the values by spaces, because to get a nice "tabbed" output I'd have to hack it together so I know the length of the string, adjust the output depending on that all the time. Is there an easier way?

Cheers,

Last edited by Fackamato (2012-01-08 01:22:33)

Offline

#2 2012-01-05 15:48:25

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Text formatting in bash

Pipe the output to 'column', e.g..

[karol@black ~]$ echo -e "foo\tbar\tbaz\n1\t12\t123"
foo	bar	baz
1	12	123
[karol@black ~]$ echo -e "foo bar baz\n1 12 123" | column -t
foo  bar  baz
1    12   123

Offline

#3 2012-01-05 16:04:49

Fackamato
Member
Registered: 2006-03-31
Posts: 579

Re: [solved] Text formatting in bash

karol wrote:

Pipe the output to 'column', e.g..

[karol@black ~]$ echo -e "foo\tbar\tbaz\n1\t12\t123"
foo	bar	baz
1	12	123
[karol@black ~]$ echo -e "foo bar baz\n1 12 123" | column -t
foo  bar  baz
1    12   123

Jesus, how could I have missed this. Thanks a lot, I'll give it a try and report back!

Offline

#4 2012-01-05 16:54:55

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: [solved] Text formatting in bash

Another idea:

$ while read -r line; do 
    printf "%3s %3s %3s\n" $line
done < <(echo -e "foo bar baz\n1 12 123")
foo bar baz
  1  12 123

Remember not to quote your var (ie $line).

Last edited by steve___ (2012-01-05 16:57:44)

Offline

#5 2012-01-05 17:12:48

hesse
Member
Registered: 2011-12-08
Posts: 88

Re: [solved] Text formatting in bash

karol wrote:

Pipe the output to 'column', e.g..

[karol@black ~]$ echo -e "foo\tbar\tbaz\n1\t12\t123"
foo	bar	baz
1	12	123
[karol@black ~]$ echo -e "foo bar baz\n1 12 123" | column -t
foo  bar  baz
1    12   123

Don't mean to bumb in here, but thanks karol that column command is really helpful.

Offline

#6 2012-01-06 10:09:43

Fackamato
Member
Registered: 2006-03-31
Posts: 579

Re: [solved] Text formatting in bash

Thanks guys. SATA check:

x@x:~/dev/checkhost$ sudo ./checkhost.sh --sata
x.laptop.x.com
 10:07:47 up  1:03,  4 users,  load average: 0.02, 0.13, 0.43

===> Checking SATA HDDs with smartctl:
Port      SN/FW                 Size   Pending_sct  Reall_sct  Uncor_sct  Offline_sct  End2end  CRC  MZONE  RAW  SMART
/dev/sda  12341234123413223423  320GB  0            0          0          0            n/a      0    0      0    OK
/dev/sdb  OCZ-1234123412413233  60GB   0            0          0          0            n/a      0    0      0    OK
INFO: 2 HDDs found

Obviously I had to output all text to a file, which I run column on afterwards. Not run column on a per-line basis (stupid me)

Offline

#7 2012-01-07 22:49:57

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Text formatting in bash

Please remember to mark the thread as solved :-)

Offline

#8 2012-01-08 01:22:41

Fackamato
Member
Registered: 2006-03-31
Posts: 579

Re: [solved] Text formatting in bash

karol wrote:

Please remember to mark the thread as solved :-)


Cheers

Offline

Board footer

Powered by FluxBB