【问题标题】:grep for files through another list of filesgrep 通过另一个文件列表查找文件
【发布时间】:2014-08-23 11:54:26
【问题描述】:

我正在尝试查找是否在调度程序中实际调用的脚本文件列表中调用了某些 java 类。

while read j; do
    while read b; do
        #Read through job files
        if [ $(cat crontab.now *.sh | grep -c ./$b) == "1" ] ; then
            echo -e $j: $(cat $b | grep -c .$j) >> bash_output.txt
        fi
    done <UsedBash.txt
done <java_file_names.txt

我只想在调度程序中找到至少一次的脚本中查找 java 文件名。没有嵌套的while循环,有没有更有效的方法来做到这一点?我也不认为我的 if 语句有效。谢谢。

【问题讨论】:

  • 您能否提供一些涉及文件的示例输入?
  • 想通了。不管怎么说,多谢拉。输入只是由新行分隔的文件名。

标签: bash loops while-loop grep find


【解决方案1】:

我不知道你真的想怎么做,但不妨试试这个:

while read j; do
    while read b; do
        [[ $(exec grep -c "/$b" crontab.now *.sh) -ge 1 ]] && \
            echo -e  "$j: $(exec grep -c ".$j" "$b")" >> bash_output.txt
    done < UsedBash.txt
done < java_file_names.txt

【讨论】:

  • 修复了代码。最终减少了一个数量级。我已经在顶部发布了完成的代码。感谢您的帮助。
  • @HarishV 您应该发布完成的代码作为答案并接受它。
【解决方案2】:

回答:

input=$(cat UsedBash.txt | tr '\n' ' ')

while read j; do

j=$(echo $j | sed -e 's/\r//g')
#Read through job files
echo $j: $(cat $input | grep -c $j) >> bash_output.txt

done &lt;java_file_names.txt

【讨论】:

    猜你喜欢
    • 2017-09-09
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多