[英]Make all files in a folder as long as the longest file
I've got many .txt
files that contain many lines with a single column of data.我有很多
.txt
文件,其中包含多行和一列数据。 However, the length of the files are not the same.但是,文件的长度是不一样的。 Some files have 2000 lines, others 2001 lines etc.
有些文件有 2000 行,有些文件有 2001 行等等。
Sometimes there's a single blank line at the end which I want to remove.有时最后有一个空行,我想删除它。
I want to append lines of 0 to the end of all but the longest file, so that the line numbers of all files are equal.我想将 0 行附加到除最长文件之外的所有文件的末尾,以便所有文件的行号相等。
I would like this to be solved with a simple shell script.我希望用一个简单的 shell 脚本来解决这个问题。
First you have to find the file with the most lines.首先,您必须找到行数最多的文件。 After that, you can append lines to the other files until they have as many lines as the file with the most lines.
之后,您可以将行附加到其他文件,直到它们的行数与行数最多的文件一样多。
Here we append lines containing the text 0
until the file has as many lines as the file with the most lines.在这里,我们添加包含文本
0
行,直到文件的行数与行数最多的文件一样多。
To remove a blank line at the ending first, you can use sed
:要首先删除结尾处的空行,您可以使用
sed
:
sed -i '${/^$/d}' /home/kisa/data/*.txt
max=$(wc -l /home/kisa/data/*.txt | head -n-1 | sort -n | awk 'END {print $1}')
for f in /home/kisa/data/*.txt; do
n=$(wc -l < "$f")
yes 0 | head -n $((max-n)) >> "$f"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.