Give tab spacing after first word in text file
Consider I have a .txt file as of the format,
Name: Abc
Id: 123
Age: 12
Address: xyz
I want to convert this file of the form,
Name : Abc
Id : 123
Age : 12
A开发者_Python百科ddress : xyz
i.e the Colon after each tile should move 2 or 3 tab spaces. The title will be only a single word and wont have spaces. So the title wont be of the form(Your age:). So i can just read the first word and give tabs after that.
How can i do this? and will doing in perl be simpler or any other language.?
Find the length of the biggest word, add n spaces to it. That's the padding to use when formatting each word.
PHP, for example, has a sprintf() function that you can look at. Various other languages provide formatted output as well.
In PHP, the following format should work - "%-<max>s : %s"
, where <max>
is the length of the biggest word + some padding.
sprintf("%-25s : %s", $key, $value);
-
makes the string left aligned.
精彩评论