Linux shell逐行读取文件的方法

本文对比了Linux下四种不同的文件读取方法:while循环、重定向法、文件描述符法及for循环,并通过实验验证了它们的执行效率。

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

方法1:while循环中执行效率最高,最常用的方法。
 
function while_read_LINE_bottm(){
 While read LINE
 do
echo $LINE
done  < $FILENAME
}
 
       
 
方法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<&0 。第二部将输入文件放送到文件描述符0,即标准输入。
  
方法4    for  循环。
 
function  for_in_file(){
For  i  in  `cat $FILENAME`
do
echo $i
done
}
        
 
    对各个方法进行测试,看那方法的执行效率最高。
 
          首先我们用脚本(脚本见附件)生成一个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

方式执行效率最高。











本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1585224,如需转载请自行联系原作者
Linux中,可以使用Shell脚本逐行读取文件内容。以下是几种常见的方法: ### 方法1:使用 `while` 循环和 `read` 命令(推荐) ```bash #!/bin/bash filename="example.txt" # 替换为你的文件名 while IFS= read -r line; do echo "$line" # 处理每一行 done < "$filename" ``` **说明**: - `IFS=` 防止行首和行尾的空白字符被截断。 - `-r` 防止反斜杠转义被解释。 - `line` 变量存储当前行的内容。 - `done < "$filename"` 表示从文件读取输入。 --- ### 方法2:使用 `for` 循环(不推荐处理复杂行) ```bash #!/bin/bash filename="example.txt" for line in $(cat "$filename"); do echo "$line" # 注意:会拆分包含空格的行 done ``` **缺点**: 无法正确处理包含空格或特殊字符的行。 --- ### 方法3:使用 `awk` ```bash #!/bin/bash filename="example.txt" awk '{print}' "$filename" # 逐行处理(可替换为其他操作) ``` **扩展**: 可以在 `{}` 中添加自定义逻辑,例如: ```bash awk '{if (length($0) > 10) print "Long line:", $0}' "$filename" ``` --- ### 方法4:使用 `sed` ```bash #!/bin/bash filename="example.txt" sed -n 'p' "$filename" # 逐行打印(类似cat) ``` **扩展**: 结合其他命令(如替换、删除行): ```bash sed -n '/pattern/p' "$filename" # 只打印匹配模式的行 ``` --- ### 最佳实践 **推荐使用 `while + read`**,因为它能正确处理所有字符(包括空格、换行符、特殊符号)。 完整脚本示例: ```bash #!/bin/bash if [ $# -ne 1 ]; then echo "Usage: $0 <filename>" exit 1 fi file="$1" if [ ! -f "$file" ]; then echo "Error: File '$file' not found!" exit 2 fi while IFS= read -r line; do # 在这里处理每一行(例如:打印、解析、过滤等) echo "Processing: $line" done < "$file" ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值