shell 读取文件内容

来源:互联网 发布:知乎 剑侠叁 编辑:程序博客网 时间:2024/05/21 09:39

1、准备数据文件

$cat a.txt

200:2

300:3

400:4

500:5

 

2、用while循环从文件中读取数据

#!/bin/ksh

while read line

do

    echo $line

done < a.txt

 

运行shell,结果如下:

200:2

300:3

400:4

500:5

 

3、使用IFS读文件

说明:默认情况下IFS是空格,如果需要使用其它的需要重新赋值

#!/bin/ksh

IFS=:

while read field1 field2

do

    echo $field1$field2

done < a.txt

 

运行shell,结果如下:

2002

3003

4004

5005

0 0
原创粉丝点击