一个简单的 grep 看起来可以解决问题:
#!/bin/bash
echo -n "First number : "
read n1
echo -n "Second number : "
read n2
# Match lines containing first number, any number of characters, and second number.
grep "$n1.*$n2" your_file
但是这段代码有点过于简单了。也许你想输入像 3 和 1 这样的整数......在这种情况下,每行包含一个 3 后跟一个 1 的地方都会匹配。考虑到这一点,这里有一个解决方案:
#!/bin/bash
echo -n "First number : "
read n1
echo -n "Second number : "
read n2
# Adding decimal separators in case of whole numbers
if [[ $n1 != *.* ]]
then
n1=${n1}\\.
fi
if [[ $n2 != *.* ]]
then
n2=${n2}\\.
fi
# Match lines containing first number, any number of characters, and second number.
grep "$n1.*$n2" your_file