将给定数字与 Bash 文件夹中所需文件第一列中的数字进行比较

将给定数字与 Bash 文件夹中所需文件第一列中的数字进行比较

我正在尝试将一个数字与文件夹中所需文件第一列中的数字进行比较。我当前的脚本仅在该文件中有单行时才有效。只要任何文件有多于一行,它就会失败。

cat 192.168.1.2_time-final2_timepls
86.6333 /home/fes/nginx/Templates/test.default

cat 192.168.1.3_time-final2_timepls
8 /home/fes/nginx/createfile
122 /home/fes/nginx/Templates/rtPortal.default

cat 192.168.1.4_time-final2_timepls
981 /home/fes/nginx/Templates/test.default

我的代码:

dir1="/home/user1/test/time-final/*_time-final2_timepls"

y=100

for files3 in $dir1; do

        z=$(cat $files3 | awk '{print $1}')

        if awk 'BEGIN{exit ARGV[1]>ARGV[2]}' "$z" "$y"
                then
                  echo "z is smaller than y"
        else
                  echo "z greater than y" "Filepath: "  $files3
        fi


done

实际输出:

[root@user1]# ./my-script.sh
z smaller than y
z greater than y Filepath:  /home/user1/test/time-final/192.168.1.3_time-final2_timepls
z greater than y Filepath:  /home/user1/test/time-final/192.168.1.4_time-final2_timepls

我正在尝试获取此输出,如果满足条件 z>y,它还将打印文件中的行:

z smaller than y 
z smaller than y 
z greater than y Filepath:  /home/user1/test/time-final/192.168.1.3_time-final2_timepls    Filename: /home/fes/nginx/Templates/rtPortal.default 
z greater than y Filepath:  /home/user1/test/time-final/192.168.1.4_time-final2_timepls    Filename: /home/fes/nginx/Templates/test.default

答案1

无论如何,我从之前的解决方案中得到了答案:

y=100

awk '{print $1, $2}' *_time-final2_timepls |
        while read -r num1 path1; do
        z=$num1
        if awk 'BEGIN{exit ARGV[1]>ARGV[2]}' "$z" "$y"
                then
                  echo "z is smaller than y"
        else
                  echo "z greater than y" "Filepath: "  $path1
        fi    
done

输出:

z is smaller than y
z is smaller than y
z greater than y Filepath:  /home/fes/nginx/Templates/rtPortal.default
z greater than y Filepath:  /home/fes/nginx/Templates/test.default

相关内容