Register
It is currently Sat Nov 01, 2014 12:12 am

Echo in columns


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Sep 29, 2011 10:12 am   

Joined: Sat Sep 10, 2011 11:48 pm
Posts: 7
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.


Top
 Profile  
 PostPosted: Thu Sep 29, 2011 9:31 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
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.


Top
 Profile  
 PostPosted: Fri Sep 30, 2011 3:29 am   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
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


Top
 Profile WWW  
 PostPosted: Fri Sep 30, 2011 6:36 am   

Joined: Sat Sep 10, 2011 11:48 pm
Posts: 7
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


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP