[英]Shell script to read values from a file and to compare them with another value
我需要一个shell脚本,在其中读入一个数字,并将其与另一个文件中的数字进行比较。 这是一个例子:
我有一个名为numbers.txt的文件,其中包含以下内容:
name;type;value;description
samsung;s5;1500;blue
iphone;6;1000;silver
我读了一个数字,例如1200。它应该从文件中打印出小于1200的值(在我的示例中,它应打印出1000)
这是我开始编写的代码,但我不知道如何完成。
echo " Enter a number"
read num
if [ $numbersinthefile -le $num ]; then
echo "$numbersinthefile"
我希望我正确地定义了我的问题。 有人可以帮我吗?
采用:
#!/bin/bash
echo -n "Enter the number: "
read num
awk -F\; '$3 < '$num' {print $0;}' myfile
尝试此操作,首先使用sed
删除第一行,然后使用cut
从行中获取实际数字,然后将该数字与输入进行比较。
echo " Enter a number"
read num
sed '1d' numbers.txt | while read line; do
numbersinthefile=$(echo $line | cut -d';' -f3);
if [ $numbersinthefile -lt $num ]; then
echo $line;
fi
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.