如果行的长度小于固定长度,则将0附加到Unix/bash文件中 [英] Appending 0's to a file in unix/bash if the line is less than a fixed length

查看:28
本文介绍了如果行的长度小于固定长度,则将0附加到Unix/bash文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,如果文件长度少于66个字符,则在文件的多行末尾添加0,以使总行长等于66.

I am looking for a way in which to append 0's to the end of multiple lines in a file if they are less than 66 characters in length so that the total line length equals 66.

这是文件格式的示例(...表示前56个字符):

Here's an example of the file format (... denotes preceding 56 characters):

$cat file1
...1234567891
...123456
...
...12345678

理想格式:

...1234567891
...1234560000
...0000000000
...1234567800

推荐答案

GNU awk 解决方案:

$ awk '{s=$0; while(length(s)<66) s=s "0"; print s}' file1

或更短:

$ awk '{while(length<66) $0=$0 "0"}1' file1

具有提供的输入:

$ awk '{while(length<66) $0=$0 "0"}1' file1
...123456789100000000000000000000000000000000000000000000000000000
...123456000000000000000000000000000000000000000000000000000000000
...000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
...123456780000000000000000000000000000000000000000000000000000000

这篇关于如果行的长度小于固定长度,则将0附加到Unix/bash文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆