zoukankan      html  css  js  c++  java
  • shell脚本,按行读取文件的几种方法。


    第一种方法用while实现按读取文件。
    [root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee [root@localhost wyb]# cat anhang.sh #!/bin/bash cat a.txt| while read line do echo $line sleep 1 done [root@localhost wyb]# bash anhang.sh 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee [root@localhost wyb]# [root@localhost wyb]# cat anhang.sh #!/bin/bash while read line do echo $line sleep 1 done<a.txt [root@localhost wyb]# bash anhang.sh 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee [root@localhost wyb]# [root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee

    #第二种方法用for循环来按行读取文件,注意:用for有一个小bug,不是一行一行来读,是按空格来分。(如果一整行没有空格,就会正常显示。) [root@localhost wyb]#
    cat foranhang.sh #!/bin/bash for line in `cat a.txt` do echo $line sleep 1 done [root@localhost wyb]# bash foranhang.sh 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 eeeeee [root@localhost wyb]#
  • 相关阅读:
    DIY组装机
    伯努利数学习笔记的说...
    心得分享 | 软件研发效能(1)
    开发板烧录教程
    解决Windows7/10系统连接网线后显示“未识别的网络”的问题
    雷达扫描
    经验学习
    1045 Access denied for user 'root'@'localhost' (using password:YES)
    json格式化工具
    mysql安装出现error Nr.1045
  • 原文地址:https://www.cnblogs.com/wangyuebo/p/5824976.html
Copyright © 2011-2022 走看看