我在文本文件中有这个输入数据
1-22-2
1-22-3
1-22-17-21-16-12-4
1-22-17-20-15-12-4
// .. a lot more lines (see revision)
22-17-20-15-12-4-6-8-10-13-18
22-17-21-16-12-4-6-7-9-5-19
22-17-20-15-12-4-6-7-9-5-19
22-17-21-16-12-15-20
22-17-20
22-17-21
22-17-20-15-12-16
我希望输出文件与输入文件相同,但设计的程序应该读取每个输入文件行的最大长度。我想让我的文件具有固定长度。因此,每行读取都应该达到最大长度。
答案 0 :(得分:1)
So, your code needs to do this: Open the file1 for reading Open another file2 for writing Use a while loop to get each line Use the substr function to get only the first n characters of each line Write the modified line to file2 End Loop Close file2 Close file1
答案 1 :(得分:0)
在2个单行中执行此操作:
perl -lne "$l = $l < length $_ ? length $_ : $l; END { print qq{$l\n}}" file.txt
应该打印最大字符。说它是40。
perl -i.bak -lne "printf qq{%-40s\n}, $_" file.txt