Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement grep一个文件,找到多个单词并相应地采取行动_If Statement_Grep - Fatal编程技术网

If statement grep一个文件,找到多个单词并相应地采取行动

If statement grep一个文件,找到多个单词并相应地采取行动,if-statement,grep,If Statement,Grep,我对Linux非常陌生,不懂编程。 但是通过阅读一些我能理解的东西,这样我就能够提出问题 我在做下面的事情 在一个文件中找到几个单词,并在另一个文件中采取相应的操作 find in log.txt if "not found" 1 > notify.txt if "reset by peer" 2 > notify.txt if "Permission denied" 3 > notify.txt if "Fetching" 0 > notify.txt exit 像

我对Linux非常陌生,不懂编程。 但是通过阅读一些我能理解的东西,这样我就能够提出问题

我在做下面的事情

在一个文件中找到几个单词,并在另一个文件中采取相应的操作

find in log.txt
if "not found" 1 > notify.txt
if "reset by peer" 2 > notify.txt
if "Permission denied" 3 > notify.txt
if "Fetching" 0 > notify.txt
exit

请帮我写剧本

我想在notify.txt中写入0或1或2或3。 现在脚本将决定写什么。 脚本将通过读取log.txt文件来决定


谢谢

你的问题有些含糊不清,但我相信这正是你想要的:

#!/bin/bash

# Put the source file / output file in variables
# so they are easier to change
src_file=log.txt
outfile=notify.txt

# Look for the patterns in the source file with "grep":
not_found=$(grep "not found" $src_file)
reset_by_peer=$(grep "reset by peer" $src_file)
perm_denied=$(grep "Permission denied" $src_file)
fetching=$(grep "Fetching" $src_file)

# If the output file already exists, remove it
if [[ -f $outfile ]]; then
    rm -f $outfile
fi

# Create a blank output file
touch $outfile

# If any of the patterns are found, print a number accordingly:
if [[ -n $not_found ]]; then
    echo "1" >> $outfile
fi 

if [[ -n $reset_by_peer ]]; then
    echo "2" >> $outfile
fi 

if [[ -n $perm_denied ]]; then
    echo "3" >> $outfile
fi 

if [[ -n $fetching ]]; then
    echo "0" >> $outfile
fi 

由于您使用多个
if
块而不是一个
if-else if-…-else
块来构建问题,因此我假设源文件中可以存在多个模式,如果是这样,您希望在输出文件中显示多个数字。(尽管即使src文件中也只能存在一种模式,这种方法仍然有效)

如果“未找到”和“对等重设”都存在怎么办?不,我已经测试了日志文件。。这是不可能的。每次日志文件都将被覆盖,因此在更新时,所有旧文本都将被删除。它给出错误=./if.sh touch:缺少文件操作数有关详细信息,请尝试“touch--help”/if.sh:第22行:$outfile:不明确重定向**第22行是“echo”2“>>$outfile”Oops,有一个输入错误:在文件的第6行,将其从
out\u file=notify.txt
更改为
outfile=notify.txt
(从变量中删除下划线),应该可以正常工作-我刚刚测试过。很好,可以工作。谢谢你,先生。非常感谢。如果你觉得它有用的话,请考虑接受这个答案。我点击了“是”,“这篇文章对你有用吗?/我无法找到如何考虑你的答案是有益的…
#!/bin/bash

# Put the source file / output file in variables
# so they are easier to change
src_file=log.txt
outfile=notify.txt

# Look for the patterns in the source file with "grep":
not_found=$(grep "not found" $src_file)
reset_by_peer=$(grep "reset by peer" $src_file)
perm_denied=$(grep "Permission denied" $src_file)
fetching=$(grep "Fetching" $src_file)

# If the output file already exists, remove it
if [[ -f $outfile ]]; then
    rm -f $outfile
fi

# Create a blank output file
touch $outfile

# If any of the patterns are found, print a number accordingly:
if [[ -n $not_found ]]; then
    echo "1" >> $outfile
fi 

if [[ -n $reset_by_peer ]]; then
    echo "2" >> $outfile
fi 

if [[ -n $perm_denied ]]; then
    echo "3" >> $outfile
fi 

if [[ -n $fetching ]]; then
    echo "0" >> $outfile
fi