search
HomeBackend DevelopmentPython Tutorial4 ways to read files line by line in Shell

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

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

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

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

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

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

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

方法3: 文件描述符法

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

注释: 这种方法分2步骤,第一,通过将所有内容重定向到文件描述符3来关闭文件描述符0.为此我们用了语法Exec 3

方法4    for  循环。

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

注释:这种方式是通过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

执行脚本后: [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

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

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

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

while read LINE
do
echo $LINE
done < $FILENAME

方式执行效率最高。

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


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
How Do I Use Beautiful Soup to Parse HTML?How Do I Use Beautiful Soup to Parse HTML?Mar 10, 2025 pm 06:54 PM

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

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...

Mathematical Modules in Python: StatisticsMathematical Modules in Python: StatisticsMar 09, 2025 am 11:40 AM

Python's statistics module provides powerful data statistical analysis capabilities to help us quickly understand the overall characteristics of data, such as biostatistics and business analysis. Instead of looking at data points one by one, just look at statistics such as mean or variance to discover trends and features in the original data that may be ignored, and compare large datasets more easily and effectively. This tutorial will explain how to calculate the mean and measure the degree of dispersion of the dataset. Unless otherwise stated, all functions in this module support the calculation of the mean() function instead of simply summing the average. Floating point numbers can also be used. import random import statistics from fracti

Serialization and Deserialization of Python Objects: Part 1Serialization and Deserialization of Python Objects: Part 1Mar 08, 2025 am 09:39 AM

Serialization and deserialization of Python objects are key aspects of any non-trivial program. If you save something to a Python file, you do object serialization and deserialization if you read the configuration file, or if you respond to an HTTP request. In a sense, serialization and deserialization are the most boring things in the world. Who cares about all these formats and protocols? You want to persist or stream some Python objects and retrieve them in full at a later time. This is a great way to see the world on a conceptual level. However, on a practical level, the serialization scheme, format or protocol you choose may determine the speed, security, freedom of maintenance status, and other aspects of the program

How to Perform Deep Learning with TensorFlow or PyTorch?How to Perform Deep Learning with TensorFlow or PyTorch?Mar 10, 2025 pm 06:52 PM

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

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

Scraping Webpages in Python With Beautiful Soup: Search and DOM ModificationScraping Webpages in Python With Beautiful Soup: Search and DOM ModificationMar 08, 2025 am 10:36 AM

This tutorial builds upon the previous introduction to Beautiful Soup, focusing on DOM manipulation beyond simple tree navigation. We'll explore efficient search methods and techniques for modifying HTML structure. One common DOM search method is ex

How to Create Command-Line Interfaces (CLIs) with Python?How to Create Command-Line Interfaces (CLIs) with Python?Mar 10, 2025 pm 06:48 PM

This article guides Python developers on building command-line interfaces (CLIs). It details using libraries like typer, click, and argparse, emphasizing input/output handling, and promoting user-friendly design patterns for improved CLI usability.

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.