Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formating the data in a file 2

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
0
0
US
Hi perl gurus,

I am trying to format a file config.txt with the following sample data

abc=11
abcdefgh=234
a=87
abc_cde=input
khs_neo=230

I need an output as following with spaces/tabs before the "=" and a space after the "="
Code:
abc        = 11
abcdefgh   = 234
a          = 87
abc_cde    = input
khs_neo    = 230

I tried some options but cannot format it as above. If I try adding tabs on all lines before the "=", it will still be not correct because the tabs need to depend on the number of characters in the first field (before the "=")

Any help is really appreciated.

Thanks,
dbadmin


 
Just use printf or sprintf:

Code:
use strict;
use warnings;

my @values = (
	[qw(abc 11)],
	[qw(abcdefgh 234)],
	[qw(a 87)],
	[qw(abc_cde input)],
	[qw(khs_neo 230)],
);

for my $vals (@values) {
	my ($key, $val) = @$vals;
	
	printf "%-8s = %s\n", $key, $val;
}

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top