您的位置:首页 > 运维架构 > Shell

shell读取文件的每一行

2013-11-01 08:50 246 查看










shell读取文件的每一行

首先装了cygwin,在win8.1下需要安装64位的,
http://cygwin.com/setup-x86_64.exe
#!/bin/bash 很重要,必须加在最前面

有1.txt,内容如下:

1111

2222

3333 4444 555

写法一:

----------------------------------------------------------------------------

#!/bin/bash

while read line

do

echo $line

done < 1.txt

----------------------------------------------------------------------------

写法二:

----------------------------------------------------------------------------

#!/bin/bash

cat 1.txt | while read line

do

echo $line

done

----------------------------------------------------------------------------

写法三:

----------------------------------------------------------------------------

for line in `cat 1.txt`

do

echo $line

done

----------------------------------------------------------------------------

说明:

for逐行读和while逐行读是有区别的,如:

cat 1.txt

1111

2222

3333 4444 555

cat file | while read line; do echo $line; done

1111

2222

3333 4444 555

for line in $(<1.txt); do echo $line; done

1111

2222

3333

4444

555

两列读取:

#!/bin/bash

cat 1.txt | while read col1 col2

do

echo col1

echo col2

done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: