gpt4 book ai didi

linux - 在for循环bash中循环两个值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:29 27 4
gpt4 key购买 nike

下面是我的输入文件,我需要传递并找出其值是否超过10G。

100G tom
30G kelly
40G messy
50G Kyrie
1G james
.5G curry
2.4 sweety
57G muller
6G kevin

这是 bash 中的代码尝试。

for i in `cat test.txt`;
do
k=$(echo $i | awk '{print $1}' | grep G | cut -d'G' -f1;);
j=$(echo $i | grep -v '^[0-9]';);
int=${k%.*};
if [[ -z $int ]];
then continue
else
if [ $int -ge 10 ];
then
# du -sh
echo $j
# sudo du -sh $j
fi
fi
# echo $int
done

如果大于 10,它就会产生结果,即 if [ $int -ge 10 ] 正在工作。如果文件中的值大于 10Gig,我将无法打印名称。简单来说,我的预期结果是

100G tom
30G kelly
40G messy
50G Kyrie
57G muller

最佳答案

awk 'BEGIN{FS="G| "} $1>10' inputfile
100G tom
30G kelly
40G messy
50G Kyrie
57G muller

这是使用 G 作为字段分隔符,$1>10 是打印整条记录的条件。 while 空格也用作字段分隔符来处理像 2.4 sweety

这样的记录

注意:这假定您的输入仅在 Gigs 中有记录。

关于linux - 在for循环bash中循环两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46292560/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com