当前位置:网站首页>用shell来计算文本中的数字之和
用shell来计算文本中的数字之和
2022-07-27 06:10:00 【hawanglc】
今天遇到一个怪事:用wc -l *.txt > wc.out 的文件中,最后一行不是统计的所有文件的记录之和。这样的话,还需要麻烦我自己写个shell脚本,来累加一下wc.out文件中每一条记录的数字之和。
以下是对应的脚本,shell的参数是文件名,这里是wc.out。wc.out的文件格式是『 数字 文件名』。
主要思路就是:读取文件中的每一行,然后用cut命令,得到每一行中的数字,然后把这个数字累加即可。
#!/bin/bash
if [ $# -ne 1 ]
then
echo "need one parameter: file_name needed"
exit
fi
sum=0;
while read line; do amount=`echo $line|cut -d " " -f1`; sum=$(( ${amount} + $sum )) ; done < $1;
echo "total amount is $sum"
边栏推荐
- 【QT】无法在QT创建者中打开包含文件pcap.h(C1083)
- Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)
- Word wrap: break word line feed is compatible with browsers
- Synchronized锁
- 在rhel8上使用soci连接oracle和postgresql和sqlite
- Sort increment with typescript
- Please ask the big guys a question. The pgsqlcdc task can't monitor changes after running for a period of time. Just restart it. What should I do
- 在mac中使用docker来搭建oracle数据库服务器
- Codeforces Round #804 (Div. 2)(5/5)
- Drools (5): drools basic syntax (3)
猜你喜欢

Watermelon book learning Chapter 5 --- neural network

35. Search Insert Position 搜索插入位置

35. Search insert position

C4D云渲染平台选哪家合作?

在mac中使用docker来搭建oracle数据库服务器

A Competitive Swarm Optimizer for Large Scale Optimization

DDD Domain Driven Design Notes

No.0 training platform course-2. SSRF Foundation

C4D动画如何提交云渲染农场快速渲染?

Digital image processing Chapter 1 Introduction
随机推荐
PHP defines the array using commas,
Please ask the big guys a question. The pgsqlcdc task can't monitor changes after running for a period of time. Just restart it. What should I do
软件测试十大必问面试题(附答案和解析)
Gbase 8C technical features
MySQL limit paging query optimization practice
从技术原理看元宇宙的可能性:Omniverse如何“造”火星
Codeforces Round #787 (Div. 3)(7/7)
SQLite 常用功能整合
MySQL query operation index optimization practice
35. Search insert position
一个优先级顺序的SQL问题
如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
"Weilai Cup" 2022 Niuke summer multi school training camp 1
查看服务器重启前的 dmesg 日志
(posted) comparison of Eureka, consumer and Nacos 2
Tableau prep is connected to maxcompute and only writes simple SQL. Why is this error reported?
?实验 7 基于 Mysql 的 PHP 管理系统实现
Synchronized锁
Usage of string class
QT连接sqlite数据库的错误及其修改办法