一、三种方法

1.exec读取文件

exec <file
sum=0
while read line
do
cmd
done

2. cat读取文件

cat file|while read line
do
cmd
done
  • 推荐用途:

    通过awk等三剑客获取文件中的数据后,可以使用这种方法用管道抛给while按行读取

3. while循环最后加重定向

while read line
do
cmd
done<file
  • 推荐用途:

    直接按行读取文件中的内容时,推荐用此方法

二、案例

读取web日志文件,把日志文件中每行中的访问字节数相加,统计访问总量

  • cat /server/scripts/c9.sh
#!/bin/bash
sum=0
exec <$1
while read line
do
size=`echo $line|awk '{print $10}'`
expr $size + 1 &>/dev/null
if [ $? -ne 0 ];then
continue
fi
((sum=sum+$size))
done
echo "${1}:total:${sum}bytes =`echo $((${sum}/1024))`KB"

其他2-while+read按行读取文件的更多相关文章

  1. C++/Php/Python/Shell 程序按行读取文件或者控制台

    写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...

  2. Python跳过第一行读取文件内容

    Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...

  3. python_基础学习_01_按行读取文件的最优方法

    python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...

  4. Java利用内存映射文件实现按行读取文件

    我们知道内存映射文件读取是各种读取方式中速度最快的,但是内存映射文件读取的API里没有提供按行读取的方法,需要自己实现.下面就是我利用内存映射文件实现按行读取文件的方法,如有错误之处请指出,或者有更好 ...

  5. python 按每行读取文件怎么去掉换行符

    python按每行读取文件后,会在每行末尾带上换行符,这样非常不方便后续业务处理逻辑,需要去掉每行的换行符,怎么去掉呢?看下面的案例: >>> a = "hello wor ...

  6. Shell按行读取文件的3种方法

    Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: [root@mini05 -]# cat file.info 写法一: [root@mini05 -]# cat read1. ...

  7. Python按行读取文件、写文件

    Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...

  8. shell脚本,按行读取文件的几种方法。

    第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...

  9. C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。

    C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...

  10. C++ 按行读取文件并打印

    #include<iostream> #include<fstream> #include<string> #include <vector> #inc ...

随机推荐

  1. unity 扇形范围检测目标

    第一种 代码方法 传入目标点测试即可 private float ScopeDistance = 2f;//扇形距离 private float ScopeJiaodu = 120;//扇形的角度 / ...

  2. #pragma directive

    #pragma package(smart_init) #pragma package(smart_init)确保已打包的单元按照其依赖关系确定的顺序进行初始化(默认情况下包含在package(包)源 ...

  3. springboot aop本地缓存防止重复提交

    实现原理: 自定义防止重复提交标记(@RepeatSubmit). 对需要防止重复提交的Congtroller里的mapping方法加上该注解. 新增Aspect切入点,为@RepeatSubmitA ...

  4. vue路由中 Navigating to current location ("/xxx") is not allowed

    原因:报错原因:多次点击同一路由,导致路由被多次添加 解决:在引入路由的地方 import Vue from 'vue' import Router from 'vue-router' Vue.use ...

  5. Java基础——IO模型详解

  6. (python)正则表达式

    # -*- coding: utf-8 -*- import re def test_dot(): """ . => 表示匹配任意字符 ""&q ...

  7. 常见的abd命令

    https://blog.csdn.net/qq_34512207/article/details/125283285

  8. error: the option `Z` is only accepted on the nightly compiler

    问题记录 $ cargo expand Checking helo v0.1.0 (/Users/Buzz/Documents/git/rust-lang/hello) error: the opti ...

  9. nvim比较两个文件的不同

    vim -d file1 file2 或 vimdiff file1 file2 2. 如果已经打开了文件file1,再打开另一个文件file2进行比较: :vert diffsplit file2 ...

  10. COM三大接口:IUnknown、IClassFactory、IDispatch。

    (1)COM组件有三个最基本的接口类,分别是IUnknown.IClassFactory.IDispatch. COM规范规定任何组件.任何接口都必须从IUnknown继承,IUnknown包含三个函 ...