如何在Unix中文件的每一行末尾添加100个空格 [英] How to add 100 spaces at end of each line of a file in Unix
问题描述
我有一个文件,该文件应该每行包含200个字符.我收到的源文件每行只有100个字符.我现在需要为每行添加100个额外的空格.如果只有几个空格,我们可以像这样使用sed:
I have a file which is supposed to contain 200 characters in each line. I received a source file with only 100 characters in each line. I need to add 100 extra white spaces to each line now. If it were few blank spaces, we could have used sed like:
sed 's/$/ /' filename > newfilename
既然有100个空格,谁能告诉我可以在Unix中添加吗?
Since it's 100 spaces, can anyone tell me is it possible to add in Unix?
推荐答案
如果您希望每行固定n个字符(不相信输入文件每行有确切的m个字符),请按照以下步骤操作.对于每行字符数不同的输入文件:
If you want to have fixed n chars per line (don't trust the input file has exact m chars per line) follow this. For the input file with varying number of chars per line:
$ cat file
1
12
123
1234
12345
每行扩展到10个字符.
extend to 10 chars per line.
$ awk '{printf "%-10s\n", $0}' file | cat -e
1 $
12 $
123 $
1234 $
12345 $
显然在脚本中将10更改为200.这里的$
显示了行尾,它不是字符.您不需要cat -e
,这里只是显示行已扩展.
Obviously change 10 to 200 in your script. Here $
shows end of line, it's not there as a character. You don't need cat -e
, here just to show the line is extended.
这篇关于如何在Unix中文件的每一行末尾添加100个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!