Home  >  Article  >  Backend Development  >  3 ways to read files line by line in shell

3 ways to read files line by line in shell

高洛峰
高洛峰Original
2017-01-09 13:45:001785browse

There are many methods, here are three methods:
Writing method one:

#!/bin/bash
while read line
do
echo $line
done < filename(待读取的文件)

Writing method two:

#!/bin/bash
cat filename(待读取的文件) | while read line
do
echo $line
done

Writing method three:

for line in `cat filename(待读取的文件)`
do
echo $line
done

Explanation:# There is a difference between ##for line-by-line reading and while line-by-line reading, such as:

$ 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

For more 3 methods of shell reading files line by line, please pay attention to the PHP Chinese website for related articles !

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn