今天写了一个简单的 Linux Shell 逐行读取文件(txt,sh,csv....)的程序,记录一下,有需要的朋友可以参考。

#!/bin/bash

# Only 1 parameter !

if [ $# != 1 ];then

echo " Usage: .\read.sh filename!";

exit

fi

# check the file !

if ! [ -f $1 ];then

echo "file does not exist!"

exit

elif ! [ -r $1 ];then

echo "file can not be read !"

exit

fi

# PRESS ANY KEY TO CONTITUE !

read -p "begin to read $1 "

# set IFS="\n" , read $1 file per line !

IFS="

"

# i is the line number

i=1

for line in `cat $1`

do

echo line $i:$line

let "i=$i+1"

done

echo "Finished reading file by line ! "

程序虽然简单,但是细节问题容易出错,从而浪费宝贵的时间~

PS : 读取到每一行的数据之后,可以配合使用 cut 命令解析这一行的数据~

Logo

更多推荐