转载——怎样一行一行读文件

原文地址:

用bash脚本读文件的方法有很多。请看第一部分,我使用了while循环及其后的管道命令(|)(cat $FILE | while read line; do … ),并在循环当中递增 i 的值,最后,我得到了非我所想的 i 。主要的原因是,管道命令会发起子shell来读取文件,而任何在(子shell的)while循环中的操作(例如 i ++),都会随着子shell的结束而丢失。

而第二种,也是最坏的一种,其最明显的错误就是在读文件的过程中使用了for循环(for fileline in $(cat $FILE);do ..),这样每打印一个单词就换一次行,因为for循环使用空格作为默认的IFS。

完美的方法,即第三种的while循环(while read line;do …. done < $FILE) 是最合适且最简单的一行行地读文件的方法。请看以下例子。

Input: $ cat sample.txt
This is sample file
This is normal text file

Source: $ cat readfile.sh
#!/bin/bash

i=1;
FILE=sample.txt

# Wrong way to read the file.
# This may cause problem, check the value of ‘i‘ at the end of the loop
echo "###############################"
cat $FILE | while read line; do
        echo "Line # $i: $line"
        ((i++))
done
echo "Total number of lines in file: $i"

# The worst way to read file.
echo "###############################"
for fileline in $(cat $FILE);do
        echo $fileline
done

# This is correct way to read file.
echo "################################"
k=1
while read line;do
        echo "Line # $k: $line"
        ((k++))
done < $FILE
echo "Total number of lines in file: $k"

Output: $ ./readfile.sh
###############################
Line # 1: This is sample file
Line # 2: This is normal text file
Total number of lines in file: 1
###############################
This
is
sample
file
This
is
normal
text
file
################################
Line # 1: This is sample file
Line # 2: This is normal text file
Total number of lines in file: 3

转载——怎样一行一行读文件

时间: 2024-08-04 05:39:46

转载——怎样一行一行读文件的相关文章

Java8读文件仅需一行代码

JDK7中引入了新的文件操作类java.nio.file.File,它包含了很多有用的方法来操作文件,比如检查文件是否为隐藏文件,或者是检查文件是否为只读文件.开发者还可以使用Files.readAllBytes(Path)方法把整个文件读入内存,此方法返回一个字节数组,还可以把结果传递给String的构造器,以便创建字符串输出. 此方法确保了当读入文件的所有字节内容时,文件属性是关闭的,否则就会出现IO异常或其它的未检查异常.这意味着在读文件到最后的块内容后,无需关闭文件.要注意,此方法不适合

C++ 使用string一行一行读取文件

c++ 读取文件中的一行一行数据 通用模板: std::ifstream in(dictpath); if(!in) { std::cout << __DATE__ << " " << __TIME__ << __FILE__ << " " << __LINE__ << ": dict read error" << std::endl; exit(-1

php处理文件,一行一行的读取,并且把没用的行删除。

今天做sitemap.xml.找了个国外的网站,http://www.freesitemapgenerator.com/这个可以生成5000条数据,以前找那个只能生成500条.但是,生成的xml标签中有些是没有用的,如图: 于是想到了php处理文件,一行一行的读取,并且把没用的行删除. 代码如下: <?php  set_time_limit(0); $file=fopen('sitemap.xml','r'); while (!feof($file)){   $line = fgets($fil

IO流一行一行读取TXT文件

我们在开发或者测试的时候,往往会用到读取本地txt文件内容来处理数据的情况.下面是读取本地txt文件内容,是一行一行读取.如下列txt例子 小明 20 小红 20 小亮 20 下面是代码: public void test1(){ try { String encoding="utf-8";//GBK String filePath="/demo/RegionList_zh_CN.txt";//要读取的文件路径 File file=new File(filePath

file标签之act=read(只需一行代码读出文件内容)

功能: ·        读文件的内容 用法: <file act=read[id=书包名] [enc=文件字符编码] method=str name=名称[start=int] [end=int]>相对路径的文件名</file> ·        act=read:读操作 ·        id:返回的书包名(可选,默认为标签名file) ·        enc:文件内容的字符编码(可选,默认为平台文件存储编码@{sys:file.enc}) ·        method:目

转载-Python学习笔记之文件读写

Python 文件读写 Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件读写,也需要注意编码问题:其他文件的读写及编码相关详见专题的博文. open()   close()     with open(...) as ... 看以下示例就能了解 Python 的 open() 及 close() 函数.这边调用 read()方法可以一次读取文件的全部内容,Python把

Python之路 day2 按行读文件

1 #1. 最基本的读文件方法: 2 3 # File: readline-example-1.py 4 5 file = open("sample.txt") 6 7 while 1: 8 line = file.readline() 9 if not line: 10 break 11 pass # do something 12 #一行一行得从文件读数据,显然比较慢:不过很省内存. 13 14 #在我的机器上读10M的sample.txt文件,每秒大约读32000行 15 16

Python按行读文件对比

1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1:     line = file.readline()     if not line:         break     pass # do something 一行一行得从文件读数据,显然比较慢:不过很省内存. 在我的机器上读10M的sample.txt文件,每秒大约读32000行 2. 用fileinput模块 # File

Python按行读文件 高级

1. 最基本的读文件方法: file = open("sample.txt") while 1: line = file.readline() if not line: break pass # do something 一行一行得从文件读数据,显然比较慢:不过很省内存. 在我的机器上读10M的sample.txt文件,每秒大约读32000行 2. 用fileinput模块 import fileinput for line in fileinput.input("sampl