为什么我无法分割字符串?
我想通过shell脚本读取一个文件,并逐行处理它。我想从每一行中提取 2 个字段。这是我的代码:
#!/bin/bsh
mlist=`ls *.log.2011-11-1* | grep -v error`
for log in $mlist
do
while read line
do
echo ${line} | awk -F"/" '{print $4}' #This produce nothing
echo ${line} #This work and print each line
done < $log | grep "java.lang.Exception"
done
这是输入文件中的示例行:
<ERROR> LimitFilter.WebContainer : 4 11-14-2011 21:56:55 - java.lang.Exception: File - /AAA/BBB/CCC/DDDDDDDD.PDF does not exist
如果我不使用 bsh,我可以使用 ksh,结果是相同的。我们这里没有狂欢。
I want to read a file by shell script, and process it line by line. I would like to extract 2 fields from each line. Here is my code:
#!/bin/bsh
mlist=`ls *.log.2011-11-1* | grep -v error`
for log in $mlist
do
while read line
do
echo ${line} | awk -F"/" '{print $4}' #This produce nothing
echo ${line} #This work and print each line
done < $log | grep "java.lang.Exception"
done
This is a sample line from the input file:
<ERROR> LimitFilter.WebContainer : 4 11-14-2011 21:56:55 - java.lang.Exception: File - /AAA/BBB/CCC/DDDDDDDD.PDF does not exist
If I don't use bsh, I can use ksh, and the result is the same. We have no bash here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您通过
grep "java.lang.Exception"
传递 while 循环的输出。echo $line | 的输出awk -F"/" '{print $4}' 是
CCC
。当通过grep
进行管道传输时,不会打印任何内容,因为CCC
与搜索模式不匹配。尝试删除
| grep "java.lang.Exception"
你会看到循环的输出正确。另一种方法可能是删除 while 循环,而只使用:
It's because you are passing the output of your while loop through
grep "java.lang.Exception"
.The output of
echo $line | awk -F"/" '{print $4}'
isCCC
. When this is piped throughgrep
, nothing is printed becauseCCC
does not match the search pattern.Try removing
| grep "java.lang.Exception"
and you will see the output of your loop come out correctly.An alternative approach to take might be to remove the while loop and instead just use: