使用bash计算文件中的行数并将结果添加到第一行 [英] Count lines in a file using bash and add the result in the first line

查看:48
本文介绍了使用bash计算文件中的行数并将结果添加到第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件:

10 20 30 40
50 60 60 80

通过使用

$ wc -l file.txt
2 file.txt

我得到了计数,但是我想将该结果添加到我的文本文件中.

I get the count, but I want to add that result in my text file.

我希望结果是这样的:

2
10 20 30 40
50 60 70 80

我应该怎么做才能在文本文件中添加结果?

What should I do in order to prepend the result in the text file?

我在一个文件夹中有很多这样的文件,我不想一次提供一个文本文件,而是希望同时提供所有文件.

I have many of these files in one folder, and instead of providing a single text file at a time, I want to provide all the files at the same time.

推荐答案

尝试:

echo `wc -l myfile.txt` | cat - myfile.txt > tmp && mv tmp myfile.txt 

这篇关于使用bash计算文件中的行数并将结果添加到第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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