Home Backend Development Python Tutorial 4 ways to read files line by line in Shell

4 ways to read files line by line in Shell

Jan 09, 2017 pm 01:36 PM

在Linux中有很多方法逐行读取一个文件的方法,其中最常用的就是下面的脚本里的方法,而且是效率最高,使用最多的方法。为了给大家一个直观的感受,我们将通过生成一个大的文件的方式来检验各种方法的执行效率。

方法1:while循环中执行效率最高,最常用的方法。

function while_read_LINE_bottm(){
While read LINE
do
echo $LINE
done  < $FILENAME
}
Copy after login

注释:我习惯把这种方式叫做read釜底抽薪,因为这种方式在结束的时候需要执行文件,就好像是执行完的时候再把文件读进去一样。

方法2 : 重定向法;管道法: cat $FILENAME | while read LINE

Function While_read_LINE(){
cat $FILENAME | while read LINE
do
echo $LINE
done
}
Copy after login

注释:我只所有把这种方式叫做管道法,相比大家应该可以看出来了吧。当遇见管道的时候管道左边的命令的输出会作为管道右边命令的输入然后被输入出来。

方法3: 文件描述符法

Function while_read_line_fd(){
Exec 3<&0
Exec 0<$FILENAME
While read LINE
Do
Echo $LINE
Exec 0<&<3
}
Copy after login

注释: 这种方法分2步骤,第一,通过将所有内容重定向到文件描述符3来关闭文件描述符0.为此我们用了语法Exec 3<&0 。第二部将输入文件放送到文件描述符0,即标准输入。

方法4 for 循环。

function  for_in_file(){
For  i  in  `cat $FILENAME`
do
echo $i
done
}
Copy after login

注释:这种方式是通过for循环的方式来读取文件的内容相比大家很熟悉了,这里不多说。对各个方法进行测试,看那方法的执行效率最高。

首先我们用脚本(脚本见附件)生成一个70000行的文件,文件位置在/scripts/bigfile。然后通过下面的脚本来测试各个方法的执行效率,脚本很简单,不再解释。

#!/bin/bash
FILENAME="$1"
TIMEFILE="/tmp/loopfile.out" > $TIMEFILE
SCRIPT=$(basename $0)
function usage(){
echo -e "\nUSAGE: $SCRIPT file \n"
exit 1
}
function while_read_bottm(){
while read LINE
do
echo $LINE
done < $FILENAME
}
function while_read_line(){
cat $FILENAME | while read LINE
do
echo $LINE
done
}
function while_read_line_fd(){
exec 3<&0
exec 0< $FILENAME
while read LINE
do
echo $LINE
done
exec 0<&3
}
function for_in_file(){
for i in  `cat $FILENAME`
do
echo $i
done
}
if [ $# -lt 1 ] ; then
usage
fi
echo -e " \n starting file processing of each method\n"
echo -e "method 1:"
echo -e "function while_read_bottm"
time while_read_bottm >> $TIMEFILE
echo -e "\n"
echo -e "method 2:"
echo -e "function while_read_line "
time while_read_line >> $TIMEFILE
echo -e "\n"
echo -e "method 3:"
echo "function while_read_line_fd"
time while_read_line_fd >>$TIMEFILE
echo -e "\n"
echo -e "method 4:"
echo -e "function  for_in_file"
time  for_in_file >> $TIMEFILE
Copy after login

执行脚本后: [root@localhost shell]# ./while /scripts/bigfile
脚本输出内容

method 1:
function while_read_bottm
real    0m5.689s
user    0m3.399s
sys    0m1.588s
method 2:
function while_read_line
real    0m11.612s
user    0m4.031s
sys    0m4.956s
method 3:
function while_read_line_fd
real    0m5.853s
user    0m3.536s
sys    0m1.469s
method 4:
function  for_in_file
real    0m5.153s
user    0m3.335s
sys    0m1.593s
Copy after login

下面我们对各个方法按照速度进行排序。

real    0m5.153s    method 4 (for 循环法)
real    0m5.689s    method 1  (while 釜底抽薪法)
real    0m5.853s    method 3    (标识符法)
real    0m11.612s  method 2    (管道法)
Copy after login

由此可见在各个方法中,for语句效率最高,而在while循环中读写文件时,

while read LINE
do
echo $LINE
done < $FILENAME
Copy after login

方式执行效率最高。

更多Shell逐行读取文件的4种方法相关文章请关注PHP中文网!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

See all articles