更改数据文件中的特定列值
Ex:文件:input_file.out_RD具有以下数据
6710|GB|LONDON|10
6700|GB|LONDON|20
6703|GB|LONDON|15
6876|GB|LONDON|60
现在想将列号4的值加倍,因此输出文件应为:final_file.out_RD
6710|GB|LONDON|10
6700|GB|LONDON|20
6703|GB|LONDON|15
6876|GB|LONDON|60
以下脚本有助于解决问题:
#!/bin/sh
#input file from which a specific column to be multiplied
my_file="/tmp/test/input_file.out_RD"
#multiplication factor value
multiplication_factor=2.00
while IFS= read -r line
do
#read a line from input file and redirect to temporary file tmp_file.txt
echo "$line" >tmp_file.txt
#extract the forth column value from temp_file.txt
original_forth_col_value=`awk -F "|" '{print $4}' tmp_file.txt`
#calculate the final forth column value
final_forth_col_value=$(echo "$original_forth_col_value * $multiplication_factor" | bc )
echo $final_forth_col_value
#place the final forth column value in the output file --> final_file.out_RD
awk -v fv=$final_forth_col_value -F "|" 'BEGIN {OFS=FS}{$4=fv;print}' tmp_file.txt >> final_file.out_RD
done < "$my_file"