开发者

Give tab spacing after first word in text file

开发者 https://www.devze.com 2022-12-17 02:36 出处:网络
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,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号