我的输入文件格式如下,
$('.form-control').change(function () {
$('#ph1_price').val($(this).find(':selected').data('price'));
});
我想将8th-cloumn中的所有值替换为值' 32.450'同时保持原始格式(间距)完整。即,预期输出应如下所示,
ATOM 1 Cal Cal 1 61.270 93.780 100.040 1.00 0.00
ATOM 2 Cal Cal 2 12.080 65.560 105.560 1.00 0.00
ATOM 13 Cal Cal 13 40.800 13.530 75.100 1.00 0.00
ATOM 200 Cal Cal 200 102.620 22.520 97.600 1.00 0.00
我尝试过简单的awk命令
ATOM 1 Cal Cal 1 61.270 93.780 32.450 1.00 0.00
ATOM 2 Cal Cal 2 12.080 65.560 32.450 1.00 0.00
ATOM 13 Cal Cal 13 40.800 13.530 32.450 1.00 0.00
ATOM 200 Cal Cal 200 102.620 22.520 32.450 1.00 0.00
但是,它无法保留原始格式。
有人能帮助我找到更好的方法吗,最好是用awk或gawk?
答案 0 :(得分:2)
GNU awk:
gawk '
BEGIN {FIELDWIDTHS="5 7 4 5 6 12 8 8 6 6"; OFS=""}
{$8=" 32.450"; print}
' file
输入
ATOM 1 Cal Cal 1 61.270 93.780 100.040 1.00 0.00
ATOM 2 Cal Cal 2 12.080 65.560 105.560 1.00 0.00
ATOM 13 Cal Cal 13 40.800 13.530 75.100 1.00 0.00
ATOM 200 Cal Cal 200 102.620 22.520 97.600 1.00 0.00
输出
ATOM 1 Cal Cal 1 61.270 93.780 32.450 1.00 0.00
ATOM 2 Cal Cal 2 12.080 65.560 32.450 1.00 0.00
ATOM 13 Cal Cal 13 40.800 13.530 32.450 1.00 0.00
ATOM 200 Cal Cal 200 102.620 22.520 32.450 1.00 0.00
答案 1 :(得分:0)
如果您在示例输入中一直有固定宽度的列:
$ awk '{ print substr($0,1,47) " 32.450" substr($0,55) }' f.txt
ATOM 1 Cal Cal 1 61.270 93.780 32.450 1.00 0.00
ATOM 2 Cal Cal 2 12.080 65.560 32.450 1.00 0.00
ATOM 13 Cal Cal 13 40.800 13.530 32.450 1.00 0.00
ATOM 200 Cal Cal 200 102.620 22.520 32.450 1.00 0.00
答案 2 :(得分:0)
告诉sed
抓住前7个街区,跳过第8个街区并打印7个后面的32.450
。
$ sed -r 's/(( +[^ ]+){7}) +[^ ]+/\1 32.450/' file
ATOM 1 Cal Cal 1 61.270 93.780 32.450 1.00 0.00
ATOM 2 Cal Cal 2 12.080 65.560 32.450 1.00 0.00
ATOM 13 Cal Cal 13 40.800 13.530 32.450 1.00 0.00
ATOM 200 Cal Cal 200 102.620 22.520 32.450 1.00 0.00