hi
i am reading from a file and grep'ng for some values and writing
those into another file
I am using "|" as a seperator for the values
smthing like this
printf FILE " %d| %d |%d\n",$x,$y,$z;

In file i am getting values like this
12|23|23
123|3|234
22333|223|3


but i want to make it formatted like this
12 .....|12.... |23
123 ...|3 ......|234
22333 |223 ..|3

my maximum field lenth is 5 digits.
please suggest me regarding this

Recommended Answers

All 12 Replies

Hi,

here's one way (not very clever, but it works):

@numbers = (12, 12, 23, 123, 3, 234, 22333, 223, 3);

foreach $number (@numbers) {
  # fill all numbers with whitespace to a length of 5
  $number = sprintf("%-5i", $number);
  # replace the whitespaces with .
  $number =~ s/ /./g;
  print $number, "\n";
}

Regards, mawe

I think the dots were just for representation, he probably just wants to right pad the fields with spaces.

I think the dots were just for representation, he probably just wants to right pad the fields with spaces.

yes kevin.. u r right
can you please suggest now

mawe already gave you your answer, use %-5i instead of %d as the formatter.

does the same apply well for floating point numbers too?

LSF Usage for Mar_01:3600 secs|62 |31 |93 |38.7
LSF Usage for Mar_02:3663 secs|72 |26 |98 |37.4
LSF Usage for Mar_03:6752 secs|47 |9 |56 |120.6
LSF Usage for Mar_04:0 secs|0 |0 |0 |0.0
LSF Usage for Mar_05:4104 secs|74 |5 |79 |52.0
LSF Usage for Mar_06:14365 secs|92 |24 |116 |123.8
LSF Usage for Mar_07:7551 secs|43 |7 |50 |151.0


Here is my sample report

printf USAGE "LSF Usage for $day:%-9.1F secs", $cpu_time;

I am using the above statement to print the floating point number.But still i am not getting the wanted result
The report is generating as below:
LSF Usage for Mar_01:3600.7 secs|62 |31 |93 |38.7
LSF Usage for Mar_02:3663.2 secs|72 |26 |98 |37.4
LSF Usage for Mar_03:6752.3 secs|47 |9 |56 |120.6
LSF Usage for Mar_04:0.0 secs|0 |0 |0 |0.0

hi
i tried print "%6s",$cpu_time;
ii is priting correctly as expected on STDOUT
but while using the same logic to print to a file
it is replacing white spaces
please suggest me in this regard

We can help you better if you show us the code you use.

i am using the below code
as u seggested i can format integers
but not able to format floating point numbers

printf USAGE "LSF Usage for $day:";
printf USAGE ":%9.1f", $cpu_time ;
printf USAGE "|%-6i ", $total_done_jobs;
printf USAGE "|%-5i ", $total_exited_jobs;
printf USAGE "|%-5i ", $total_jobs;

hi
any one please help in frmatting the floating point numbers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.