在批处理脚本中将`grep“字符串”| wc -l`分配到变量中

时间:2021-09-22 23:29:53

I am trying to develop a batch equivalent (windows) code of below unix script:-

我正在尝试开发以下unix脚本的批处理等效(windows)代码: -

i=`grep "ora-error" *.txt|wc -l`
if [ i -gt 0 ]; then
echo "true"
else
echo "false"
fi

tried with "find /c "ora-error" *.txt" but was not able to take the o/p in to variable and redirect the same to if condition...

尝试使用“find / c”ora-error“* .txt”但是无法将o / p输入变量并将相同的重定向到if条件...

ie. when I use "find /c "ora-error" *.txt" below is the output

即。当我使用“find / c”时,ora-error“* .txt”是输出

---------- xx.TXT: 0

---------- xx.TXT:0

---------- yy.TXT: 0

---------- yy.TXT:0

---------- zz.TXT: 0

---------- zz.TXT:0

I want to take the count and redirect valur into a IF condition.

我想取计数并将valur重定向到IF条件。

I need a window script to solve this issue.

我需要一个窗口脚本来解决这个问题。

Can any body help on this...

任何身体都可以帮助...

Thanks in advance..

提前致谢..

with regards

sree

Guys worked out a solution....

伙计们制定了一个解决方案....

find /c "ora-error" *.txt>TEST1.txt
find /c "0" TEST1.TXT>TEST2.TXT
FOR /F "TOKENS=1* DELIMS=:" %%A IN (TEST2.TXT) DO SET a=%%B
set _count=%a%
IF %_count% EQU 4 goto matched
IF not %_count% EQU 4 goto notmatched

:matched
echo we are in equal to 4 loop
pause
exit

:notmatched
echo we are in not equal to 4 loop
pause
exit

1 个解决方案

#1


0  

grep "ora-error" *.txt>/dev/null
if [ $? -eq 0 ]; then
echo "true"
else
echo "false"
fi

#1


0  

grep "ora-error" *.txt>/dev/null
if [ $? -eq 0 ]; then
echo "true"
else
echo "false"
fi