当前位置:网站首页>Use shell to calculate the sum of numbers in text
Use shell to calculate the sum of numbers in text
2022-07-27 07:25:00 【hawanglc】
A strange thing happened today : use wc -l *.txt > wc.out In the file of , The last line is not the sum of the records of all the files counted . In this case , I still need to write a shell Script , Let's add up wc.out The sum of the numbers of each record in the document .
The following is the corresponding script ,shell The parameter of is file name , Here is wc.out.wc.out The file format is 『 Numbers file name 』.
The main idea is : Read every line in the file , And then use cut command , Get the number in each line , Then add up this number .
#!/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"
边栏推荐
- Functools module
- Convert Excel to csv/csv UTF-8
- Quartus: an error is reported when adding a.V file to someone else's project
- 零号培训平台课程-1、SQL注入基础
- Flutter实战-请求封装(一)
- (2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
- 请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
- 使用pip命令切换不同的镜像源
- Tcp/ip protocol analysis (tcp/ip three handshakes & four waves + OSI & TCP / IP model)
- 用shell来计算文本中的数字之和
猜你喜欢
随机推荐
12. Integer to Roman
Oracle数据库问题
How MySQL executes query statements
MySQL optimization SQL related (continuous supplement)
Es compares the data difference between the two indexes
[Vani has a date] tail on rainy days
Codeforces Round #804 (Div. 2)(5/5)
Drools (5): drools basic syntax (3)
Go obtains the processing results of goroutine through channel
Excuse me, MySQL timestamp (6) using flick SQL is null. Is there a way to deal with this
【golang学习笔记2.1】 golang中的数组中的排序和查找
(2022牛客多校三)A-Ancestor(LCA)
UUID与secrets模块
(2022 Niuke multi school III) a-ancestor (LCA)
?实验 7 基于 Mysql 的 PHP 管理系统实现
腾讯云服务器SSH链接自动断开解决方法
Oracle database problems
oracle的触发器的使用举例
Word wrap: break word line feed is compatible with browsers
Basic functions and collections of guava









