在grep的IF语句 [英] grep in IF statement

查看:147
本文介绍了在grep的IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的脚本部分读取$每一行下一步。但是,当我尝试在$到grep特定的模式,即MO下一步,显示在标准输出误差

The following script portion read each line in $next. But when I try to grep particular pattern i.e. "MO" in $next, the error is shown on standard output as

的grep:40922 | OPR:没有这样的文件或目录结果
grep的:MO:没有这样的文件或目录结果
grep的:12345 | OPR:没有这样的文件或目录结果
grep的:MO:没有这样的文件或目录结果
grep的:12345 | 12345 | 202 |地方| LMNO:没有这样的文件或目录

grep: 40922|OPR: No such file or directory
grep: MO: No such file or directory
grep: 12345|OPR: No such file or directory
grep: MO: No such file or directory
grep: 12345|12345|202|local|LMNO: No such file or directory

cat /home/scripts/$E1.out | while read next  
do  
i=`echo $next | awk -F"|" '{print($1)}'`
j=`echo $next | awk -F"|" '{print($2)}'`
k=`echo $next | awk -F"|" '{print($3)}'`
l=`echo $next | awk -F"|" '{print($4)}'`
m=`echo $next | awk -F"|" '{print($5)}'`
n=`echo $next | awk -F"|" '{print($6)}'`
o=`echo $next | awk -F"|" '{print($6)}'`  
if grep -q "MO" $next  
then echo "FOUND;" >> /home/scripts/sql.$E1.out  
else echo "NOT FOUND;" >> /home/scripts/sql.$E1.out  
fi  
done  

$ E1.out文件如下:

$E1.out files looks like :

40922 | OPR MO 12345 | OPR MO 12345 | 12345 | 202 |地方| LMNO

40922|OPR MO 12345|OPR MO 12345|12345|202|local|LMNO

推荐答案

您到 <$ C传递参数$ C>的grep $下一,被视为文件名进行搜索列表。如果您想该行中搜索字符串,比方说, MO ,那么你需要要么把它放在一个文件并通过该文件作为参数,或管它通过标准输入。

The argument you pass in to grep, $next, is being treated as a list of filenames to search through. If you would like to search within that line for a string, say, MO, then you need to either put it in a file and pass that file in as an argument, or pipe it in via standard input.

下面是一个例子,你可以尝试在命令行上;当然,替换你正在使用的字面意义,我包括说明变量:

Here's an example that you can try out on the command line; of course, substitute the variable that you're using for the literal value that I included to illustrate:

if echo "40922|OPR MO 12345|OPR MO 12345|12345|202|local|LMNO" | grep -q "MO"
  then echo "FOUND"
  else echo "NOT FOUND"
fi

这篇关于在grep的IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆