简体   繁体   English

使文件夹中的所有文件与最长的文件一样长

[英]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.

相关问题 将所有文件(位于不同文件夹中)放在一个文件中 - Make all files ( that are in diffrent folders ) in one file 重命名文件夹中的所有文件 - Rename all files in a folder 比较文件夹中的所有文件 - Compare all files in a folder 用于从文件夹中的所有文件中删除文件名中的字符的脚本 - Script to remove characters from a file name for all files in a folder Bash脚本处理文件夹中的所有文件并将所有输出保存到具有相同文件名的不同文件夹中? - Bash script to process all files in folder and save all output in different folder with same file names? 将文件夹中的所有文件复制到另一个以 . 到文件名并将其重命名回原始文件名 - Copy all files in folder to another folder prepending a . to the file name and rename it back to original file name BASH:将一个文件夹的所有文件复制到另一个文件夹 - BASH: copy all the files of a folder to other folder 杀死文件夹中的所有pid文件 - Kill all pid files in a folder 成对比较文件夹的所有文件 - Compare all files of a folder pairwise Mac OSX Unix Bash脚本将文件夹中的所有文件捕获到一个文本文件中,最好使用文件顶部的标签 - Mac OSX Unix Bash Script to cat all files in a folder to one text file, preferrably with a label at the top of the file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM