shell按行读取文件的3种方法
2016-09-13 17:56
295 查看
方法有很多,下面写出三种方法:
写法一:
复制代码代码如下:
#!/bin/bash
while read line
do
echo $line
done < filename(待读取的文件)
写法二:
复制代码代码如下:
#!/bin/bash
cat filename(待读取的文件) | while read line
do
echo $line
done
写法三:
复制代码代码如下:
for line in `cat filename(待读取的文件)`
do
echo $line
done
说明:
for逐行读和while逐行读是有区别的,如:
复制代码代码如下:
$ cat file
1111
2222
3333 4444 555
$ cat file | while read line; do echo $line; done
1111
2222
3333 4444 555
$ for line in $(<file); do echo $line; done
1111
2222
3333
4444
555
写法一:
复制代码代码如下:
#!/bin/bash
while read line
do
echo $line
done < filename(待读取的文件)
写法二:
复制代码代码如下:
#!/bin/bash
cat filename(待读取的文件) | while read line
do
echo $line
done
写法三:
复制代码代码如下:
for line in `cat filename(待读取的文件)`
do
echo $line
done
说明:
for逐行读和while逐行读是有区别的,如:
复制代码代码如下:
$ cat file
1111
2222
3333 4444 555
$ cat file | while read line; do echo $line; done
1111
2222
3333 4444 555
$ for line in $(<file); do echo $line; done
1111
2222
3333
4444
555
相关文章推荐
- shell按行读取文件的3种方法
- PHP按行读取文件时删除换行符的3种方法
- shell脚本按行读取文件内容的方法
- PHP按行读取文件时删除换行符的3种方法
- shell脚本从文件中按行读取数据,并且赋值到数组中的几种方法
- shell按行读取文件的常见几种方法
- PHP按行读取文件时删除换行符的3种方法
- shell编程学习之道:shell按行读取文件的方法
- PHP按行读取文件时删除换行符的3种方法
- shell按行读取文件的方法
- android按行读取文件内容的几个方法
- linux批量替换文件内容3种方法(perl,sed,shell)
- linux批量替换文件内容3种方法(perl,sed,shell)
- shell下同时读取多个文件的方法
- android按行读取文件内容的几个方法
- SHELL读取文件的方法
- shell 按行读取文件,涉及读配置文件
- Shell 编程-文件读取方法集锦
- shell脚本按行读取文件并解析