Linux 在terminal CLI或shell sciprt中,我希望计算多个文件的行数,并在每个文件的第一行写入行数
在terminal CLI或shell sciprt中,我希望计算多个文件的行数,并在每个文件的第一行写入行数 例如,a.txt、b.txt、c.txt都在一个目录中 a、 文本 b、 文本 c、 文本 a、 txt有4行 b、 txt有两行 c、 txt有3行 所以,我想如下 a、 文本 b、 文本 c、 文本 我试着用sed,但失败了。。。Linux 在terminal CLI或shell sciprt中,我希望计算多个文件的行数,并在每个文件的第一行写入行数,linux,shell,sed,terminal,command-line-interface,Linux,Shell,Sed,Terminal,Command Line Interface,在terminal CLI或shell sciprt中,我希望计算多个文件的行数,并在每个文件的第一行写入行数 例如,a.txt、b.txt、c.txt都在一个目录中 a、 文本 b、 文本 c、 文本 a、 txt有4行 b、 txt有两行 c、 txt有3行 所以,我想如下 a、 文本 b、 文本 c、 文本 我试着用sed,但失败了。。。 谢谢。我想你可以用这样的东西: for file in *.txt; do printf '%d lines are in.\n' "$(wc
谢谢。我想你可以用这样的东西:
for file in *.txt; do
printf '%d lines are in.\n' "$(wc -l < "$file")" | cat - "$file"
done
用于*.txt中的文件;做
printf“%d”行位于。\n'$(wc-l<“$file”)“| cat-“$file”
完成
wc-l
获取行数printf
使用数字生成字符串cat
将printf
的输出与文件内容结合起来
这将打印到标准输出。如果您对输出感到满意,可以将tmp&&mv tmp“$file”添加到循环内的命令中,以覆盖原始文件。我想您可以使用如下方法:
for file in *.txt; do
printf '%d lines are in.\n' "$(wc -l < "$file")" | cat - "$file"
done
用于*.txt中的文件;做
printf“%d”行位于。\n'$(wc-l<“$file”)“| cat-“$file”
完成
wc-l
获取行数printf
使用数字生成字符串cat
将printf
的输出与文件内容结合起来
这将打印到标准输出。如果您对输出满意,可以将>tmp&&mvtmp“$file”
添加到循环内的命令中,以覆盖原始文件
hello world c_1
hello world c_2
hello world c_3
4 lines are in.
hello world a_1
hello world a_2
hello world a_3
hello world a_4
2 lines are in.
hello world b_1
hello world b_2
3 lines are in.
hello world c_1
hello world c_2
hello world c_3
for file in *.txt; do
printf '%d lines are in.\n' "$(wc -l < "$file")" | cat - "$file"
done