使用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" 是删除头三行的意思, 和本文无直接关系。