`
jak47
  • 浏览: 72261 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

使用shell按行读取文件

阅读更多

在shell里面,按行读取文件得注意设置环境变量IFS,否则无法正确读入。

具体例子如下

 

OLDIFS=$IFS
IFS=$'\n'

xxx_file=/home/xx/xxx.txt
in_file=/home/xxx/xxx.in

for pattern in $(cat ${xxx_file})
do
  grep -v -e $pattern  ${in_file}
done

IFS=$OLDIFS

 

要按行读取csv文件时候,代码如下

IFS=","

sed "1,3d" ${input_file_1} | while read col1, col2, col3, col4
do
   echo "$col1, $col2, $col3"
done

IFS=$OLDIFS

 

其中 sed "1,3d" 是删除头三行的意思, 和本文无直接关系。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics