BashScripts.org
http://bashscripts.org/forum/

Echo in columns
http://bashscripts.org/forum/viewtopic.php?f=8&t=1462
Page 1 of 1

Author:  nikosliapis [ Thu Sep 29, 2011 10:12 am ]
Post subject:  Echo in columns

Hello everybody
I've made a script that reads IP's and names from a file and checks if on-line(with ping)
I use this for printing:
Code:
echo "$name---$status" >> log.log

and the result looks like this:
Router---ON
Mail_Server---ON
Pc_1---OFF
Computer_name_2---OFF
etc....

I want the result to look like this:
Router-------------------------ON
Mail_Server------------------ON
Pc_1---------------------------OFF
Computer_name_2-------OFF
etc....

How can this be done?
Thanks in advance.

Author:  Watael [ Thu Sep 29, 2011 9:31 pm ]
Post subject:  Re: Echo in columns

Hi,

you should not format log files this way.

imho, they're more like database's tables, and should have a simple format : data<separator>data<separator>etc.

when you're extracting data from log files, then it's easy to distinguish fields.

printf can easily align text on the right. To do more you'll need to count characters from line length...
see also what GNUcoreutils (if available) could be of help.

Author:  Keith Hedger [ Fri Sep 30, 2011 3:29 am ]
Post subject:  Re: Echo in columns

I agree with Watael about the separators but you could try using something like
Code:
echo -e "$name\t\t$status"
the embedded tabs are easily parsed if you need to query the log file but makes the printing neater like so
Code:
echo -e "$name\t\t$status"
SlackHolly      ON
and is easier to read than all those dashes

Author:  nikosliapis [ Fri Sep 30, 2011 6:36 am ]
Post subject:  Re: Echo in columns

Thank you all for your replies.
I agree that it is easy to extract data using separators, i use the "cut" command often.
Although i just wanted the file to be pretty.
I managed to do it using printf to put spaces between and then the "tr" command to
replace "space" with "-"
Code:
printf "%-20s %s\n" $NAME $status | tr " " "-"

Thanks again

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com