gpt4 book ai didi

linux - Grep 多个乱序的东西

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:17 27 4
gpt4 key购买 nike

我目前正致力于在 bash 中创建数据库。我想添加一个“和”功能。这个函数将搜索两个东西,并且只返回匹配两个搜索的东西。我将如何实现?

这是我的代码:

#!/bin/bash

clear

#return all
function all {
cat people.dat
}
#return some
function search {
grep -i "$SEARCH" people.dat || echo "search returned nothing"
}
#or search
function or {
egrep -i "$search1|$search2" people.dat
}
#and search
function and {
if [[ $(grep -i "$search1") = $(grep -i "$search2") ]]; then
echo yes
#that is temporary I want to see if it worked
fi
}
#return null
function null {
return
}

while [ true ]
do
#get the search
read SEARCH

#search
if [[ $SEARCH == *" OR "* ]]; then
search1=${SEARCH%" OR "*}
search2=${SEARCH##*" OR "}
or
elif [[ $SEARCH == *" AND "* ]]; then
search1=${SEARCH%" AND "*}
search2=${SEARCH##*" AND "}
and
elif [ "$SEARCH" = "all" }; then
all
elif [ "$SEARCH" = "exit" ]; then
exit
elif [ "$SEARCH" = "" ]; then
null
else
search
fi
done

最佳答案

通过管道将第一个 grep 的输出作为第二个 grep 的输入。只有同时包含 $search1$search2 的行才会被返回。

function and {
if [[ $(grep -i "$search1" people.dat | grep -i "$search2") ]]; then
echo yes
fi
}

关于linux - Grep 多个乱序的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36183557/

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