如果一行以特定名称开头,我会尝试剪切该字段,
cat $1|while read line
do
if [ "$1" = "custbills.cmp" ]; then
acc_no=`grep "^Custbills" $1 | cut -c29-43`
acc_type=`grep "^Custbills" $1 |cut -c124-125`
echo "\"${acc_no}\",\"${acc_type}\"" >> out.csv
else
echo ""
fi
done
但给出了错误,并且在输出文件中我只得到“,”。我的脚本出了什么问题
答案1
我修改了脚本并得到了结果
if [ "$1" = "custbills.cmp" ]; then
grep "^Custbills" $1 |while read line
do
acc_no=`echo "$line" | cut -c29-43`
acc_type=`echo "$line" |cut -c124-125`
echo "$acc_no","$acc_type" >> out.csv
done
else
echo ""
fi
答案2
下列:
if [ "$1" = "custbills.cmp" ]
then
grep "^Custbills" $1 | cut -c29-43,124-125 --output-delimiter="," >> out.csv
else
echo ""
fi
似乎与您答案中的代码完全相同。但你还没有解释你为什么要测试的背景"$1"
。为什么不直接说
grep "^Custbills" "custbills.cmp" | cut -c29-43,124-125 --output-delimiter="," > out.csv
?