逐行读取文件的Shell代码

gcd8的头像 gcd8 16 2015-01-21 11:31 0

 基本信息

× 1   

浏览数: 1732

分享时间: 3 年 前

linux shell 按行循环读入文件方法
#!/bin/bash
 
printf "*************************************\n"
echo " cat file while read line"
cat one.log |while read line
do
    echo $line;
done
 
printf "*************************************\n"
echo "while read line <file"
while read line
do
    echo $line;
done <one.log
 
printf "*************************************\n"
echo "for line in cat test.txt"
SAVEIFS=$IFS
IFS=$(echo -en "\n")
for line in $(cat test.txt)
do
    echo $line;
done
IFS=$SAVEIFS

前二种方法可以分别简化成一行简短的代码

cat one.log|while read line;do echo $line;done
 
while read line; do echo $line; done<one.log

还没有人评论,赶快来抢沙发吧!