【问题标题】:assinging `grep "string"|wc -l` into a variable in batch scripting将 `grep "string"|wc -l` 分配给批处理脚本中的变量
【发布时间】:2012-10-04 11:21:17
【问题描述】:

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

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

尝试使用 "find /c "ora-error" *.txt" 但无法将 o/p 放入变量并将其重定向到 if 条件...

即。当我使用 "find /c "ora-error" *.txt" 下面是输出

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

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

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

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

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

任何机构都可以帮助解决这个问题...

提前谢谢..

关于

sree

大家想出了一个解决办法....

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

【问题讨论】:

  • 您为什么不发布您的解决方案作为答案?鼓励堆栈溢出用户这样做,您甚至可以将您的答案标记为已接受的答案以帮助他人。

标签: if-statement grep batch-file wc


【解决方案1】:
grep "ora-error" *.txt>/dev/null
if [ $? -eq 0 ]; then
echo "true"
else
echo "false"
fi

【讨论】:

  • 嗨,我正在尝试在 windows 中而不是在 unix 中开发代码 -
猜你喜欢
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 2011-02-10
  • 2015-03-30
  • 1970-01-01
相关资源
最近更新 更多