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

3 ways to read files line by line in shell

高洛峰
Release: 2017-01-09 13:45:00
Original
1840 people have browsed it

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

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

Writing method two:

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

Writing method three:

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

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
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template