当前位置:网站首页>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"
边栏推荐
- 零号培训平台课程-2、SSRF基础
- Perl: 将要执行的外部命令拆分为多行
- 杂谈:跟女儿聊为啥要学好文化课
- Routing between VLANs (explanation + verification)
- functools模块
- Calledprocesserror during pre commit install
- Confluence漏洞学习——CVE-2021-26084/85,CVE-2022-26134漏洞复现
- Jmeter: interface automation test - BeanShell compares database data and return data
- How to submit C4d animation to cloud rendering farm for fast rendering?
- MySQL index failure and solution practice
猜你喜欢

Convert Excel to csv/csv UTF-8

(2022牛客多校三)A-Ancestor(LCA)

Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field

Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)

Relevant principles of MySQL index optimization

【QT】capture.obj:-1: error: LNK2019: 无法解析的外部符号 __imp_htons(解决方法)

(2022 Niuke multi school III) j-journey (Dijkstra)

No.0 training platform course-2. SSRF Foundation

sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)

整体二分?
随机推荐
Oracle数据库问题
The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars
MySQL query operation index optimization practice
Will Flink CDC constantly occupy Oracle's memory by extracting Oracle's data, and finally cause oracle-040
请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
端口转发小结
jjwt 生成token
Perl: 将要执行的外部命令拆分为多行
Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field
UUID与secrets模块
Oracle database problems
腾讯云服务器SSH链接自动断开解决方法
单臂路由(讲解+实验)
"Weilai Cup" 2022 Niuke summer multi school training camp 1
C语言程序设计 | 程序编译与预处理
Zabbix: 将收集到值映射为易读的语句
在rhel7.3中编译和使用log4cxx
临界区(critical section 每个线程中访问 临界资源 的那段代码)和互斥锁(mutex)的区别(进程间互斥量、共享内存、虚拟地址)
海康h9摄像头用xshell无法连接(没有启用ssh)
(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)