简体   繁体   English

使用bash比较txt列中的浮动数据

[英]Comparison of floating data in txt column with bash

I have thousands of files containg a column of non integers data. 我有数千个文件,其中包含一列非整数数据。 I would like to build a script that iterates through all the folders, checks if the values in the file are equal/different from 0.000000. 我想构建一个遍历所有文件夹的脚本,检查文件中的值是否等于/不同于0.000000。

The column of data are like the following one: 数据列如下所示:

-0.572650
-0.002281
-0.080300
-0.803684
-0.498033
-0.206755
-0.182819
-0.222126
-0.116538
 0.720913
 0.707343
 1.134845
-0.462461
-0.382780
-0.378505
-0.436588
-0.464654
-0.207534
-0.231861
-0.155786
-0.196779
 0.977137
 1.024751
 1.037147

I have tried something like: 我已经尝试过类似的东西:

#!/bin/bash


a=$(awk '{print $1}' tmp.txt)


if (( $(echo "$a == 0.000000" |bc -l) ));
 then
 echo "MULLIKEN MISSING"
elif (( $(echo "$a != 0.000000" |bc -l) )); 
 then
echo "OK"
fi

but it is does not work in manner I would like to. 但这不是我想要的方式。

I would like the script to be able to detect if the data in column are all equal to 0.000000 or not. 我希望脚本能够检测列中的数据是否全部等于0.000000。

I would stick just with awk: this will be quite efficient as you only need to spawn one process to handle the whole file, as opposed to calling bc for every number. 我会坚持使用awk:这将非常有效,因为您只需要生成一个进程即可处理整个文件,而不是为每个数字调用bc。

if awk '$1 == 0 {exit 0} END {exit 1}' file; then
    echo "has a zero"
else
    echo "no zeroes"
fi

EDIT: I have a logic error: the awk END block is executed when the exit command is called, so the above will always exit 1 . 编辑:我有一个逻辑错误:当调用exit命令时,将执行awk END块,因此上述内容将始终退出1 Use this instead: 使用此代替:

if awk 'BEGIN {rc = 1} $1 == 0 {rc = 0; exit} END {exit rc}' file; then
    echo "has a zero"
else
    echo "no zeroes"
fi

or, exit 1 when a zero is found. 或者,找到零时退出1。 This simplifies the awk command: 这简化了awk命令:

if awk '$1 == 0 {exit 1}' file; then
    echo "no zeroes"
else
    echo "has a zero"
fi

If you want to stick with plain bash, you can read each number from the file, and remove the dot so that you only have an integer (no floating point in bash) 如果您要坚持使用普通bash,则可以从文件中读取每个数字,然后删除点,以便仅使用一个整数(bash中没有浮点)

zero=false
while read -r value; do
    if (( 10#${value//./} == 0 )); then
        zero=true
        break
    fi
done < file

$zero && echo "has a zero" || echo "no zeroes"

The 10# is to force base-10 interpretation, so that bash does not puke on invalid octal numbers like 080000. 10#强制以10#为底的解释,因此bash不会对080000等无效的八进制数字进行呕吐。

Hi this code is quite similar to yours and it should work. 嗨,这段代码与您的代码非常相似,应该可以使用。

#!/bin/bash

a=$(awk '{print $1}' tmp.txt)

for i in ${a[@]}; do
    result=$( echo $i == 0.0 | bc -l)
    if [[ $result == "0" ]];
    then
        echo "OK"
    else
        echo "NOT OK"
    fi
done

Note that reading with AWK in this way, then using variable as array is not the safest method. 请注意,以这种方式读取AWK,然后将变量用作数组不是最安全的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM