标签:shell
shell读取文件的每一行
写法一:
----------------------------------------------------------------------------
#!/bin/bash
while read line
do
echo $line
done < filename(待读取的文件)
----------------------------------------------------------------------------
写法二:
----------------------------------------------------------------------------
#!/bin/bash
cat filename(待读取的文件) | while read line
do
echo $line
done
----------------------------------------------------------------------------
如果有需要输出含指定字符的行,可以写成
----------------------------------------------------------------------------
#!/bin/bash
cat filename(待读取的文件) | grep word(指定字符) | while read line
do
echo $line
done
----------------------------------------------------------------------------
写法三:
----------------------------------------------------------------------------
for line in `cat filename(待读取的文件)`
do
echo $line
done
----------------------------------------------------------------------------
说明:
for逐行读和while逐行读是有区别的,如:
file文件内容如下:
$ 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
本文出自 “大喵下山” 博客,请务必保留此出处http://damiao.blog.51cto.com/8194496/1913687
标签:shell
原文地址:http://damiao.blog.51cto.com/8194496/1913687