首页 > 解决方案 > 将搜索值捕获到 unix shell 脚本中的变量中

问题描述

我正在尝试将文件 1 的内容搜索到文件 2 中,如果找到该内容,则存储在 found.csv 文件中或存储在 notfound.csv 文件中

下面是我的代码,

cd /mnt/data/dobiminer/scripts

usage="Usage:sh scriptname.sh 'ToSearchFile' 'MainSearchFile' 'CR' "
Date=`date +%m%d%y%H%M%S`
File=$(<$2)
echo "File Input $2"
echo $File

if [ $# != 3 ]
then
        echo $usage
        exit 1
else
        echo > "$3-Found-$Date.csv"
        echo > "$3-NotFound-$Date.csv"
        for MasterClip in `cat $1`
        do
                echo $MasterClip
                String=$(echo "$File" | grep -x $MasterClip)
                echo $String
                if [ -z $String ];
                then
                        echo "NotFound"
                        echo $MasterClip >> "$3-NotFound-$Date.csv"
                else
                        echo "Found"
                        echo $MasterClip >> "$3-Found-$Date.csv"
                fi
        done
fi

我的猜测是下面的代码行不起作用,因为每当我运行代码时,字符串值都是空的。它没有将搜索值捕获到其中。

String=$(echo "$File" | grep -x $MasterClip)
echo $String

我尝试了多种方法,但不确定我哪里出错了。谢谢你的协助

标签: shellunixsearchgrep

解决方案


推荐阅读