- 论坛徽章:
- 0
|
各位大侠们, 我想写个小程序,需要对重复的文本的某一个字段修改,消除重复,例如:
111;222;333;
111;222;333;
111;222;333;
111;222;333;
111;222;333;
111;222;333;
111;222;333;
修改成
111;222;333;
111;222;334;
111;222;335;
111;222;336;
111;222;337;
111;222;338;
111;222;339;
把第三个字段修改成递增的,想把文件名和要修改的列数作为2个参数传进去,现在可以固定修改某个字段,但是动态传进去总是弄不好,脚本如下:
#!/bin/ksh
#change file - Change specific field for duplicat records
if [ $# != 2 ]
then
echo "param is not correct"
exit 1
fi
inputfile=$1
fldnum=$2
if [ ! -s $inputfile ]
then
echo "${inputfile} not found or size is 0"
exit 1
fi
outputfile=./output
echo "" > $outputfile
prev=0
curr=0
i=0
while read line
do
echo "read from file: $line"
if [ -z $line ]
then continue
fi
curr=$line
if [ $prev != $curr ]
then
i=0
prev=$curr
fi
tmp=`echo $line|cut -d';' -f$fldnum`
export tmp=`expr $tmp + $i`
echo "After plus, tmp: $tmp"
export fldvar="\$$fldnum"
echo "fldvar: $fldvar"
chgline=`echo $line|awk -F';' -v OFS=';' '{ENVIRON["fldvar"]=ENVIRON["tmp"]}1'`
echo "after change: $chgline"
echo $chgline >> $outputfile
i=`expr $i + 1`
done < $inputfile
上面的
chgline=`echo $line|awk -F';' -v OFS=';' '{ENVIRON["fldvar"]=ENVIRON["tmp"]}1'`
想把 ENVIRON["fldvar"] 作为动态的字段值,比如 $2,$3,但总是不成功,请教各位大虾??
|
|