我有一个包含以下数据的文件:
APPLE,FRUIT,1KG,RED,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1800.00|1900.00|1920.00|1520.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1480.00|1940.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1500.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1429.100000
ORANGE,FRUIT,2KG,GREEN,1660.00|1680.00|1700.00|1720.00|1740.00|1760.00|1900.00|1920.00|1500.00|1320.00|1340.00|1360.00|1380.00|1400.00|1420.00|1440.00|1460.00|1940.00|1800.00|940.00|1880.00|1820.00|1840.00|1860.00|1780.00|1480.00|1020.00|1040.00|1060.00|1080.00|1100.00|1120.00|1140.00|1160.00|1180.00|1200.00|1220.00|1240.00|1260.00|1280.00|1300.00|960.00|980.00|1000.00|1520.00|1540.00|1560.00|1580.00|1600.00|1620.00|1640.00,1432.100000
我想检查第 5 列中的每个值(获取第 5 列的分隔符是“,”)。与第 6 列进行比较,并将差异最小的元素放入新文件中。
预期输出为:
APPLE,FRUIT,1KG,RED,1420.00
ORANGE,FRUIT,2KG,GREEN,1440.00
我的文件有 200 多行,正在寻找一个单行命令来执行相同的操作。
答案1
如果需要 AWK 解决方案,我将提供一个。
awk -F, 'NF>4{
n=split($5,a,"|")
l=a[0];
for(i=1;i<n;i++)
if((a[i]-$6)^2<(l-$6)^2)
l=a[i];
print $1","$2","$3","$4","l
}' input > output
答案2
这是我的 Python 解决方案。
with open('/path/to/file', 'r') as f:
for line in f:
x=line.split(',')
y=x[4].split('|')
print(','.join(x[:4]) + ',' + str(min(y, key=lambda z: abs(float(z)-float(x[5])))))
如果您有类似 Pyed Piper(也称为 )的东西pyp
,您可以直接从终端提示符运行:https://code.google.com/archive/p/pyp/