简体   繁体   English

Bash脚本:跳过文件和grep

[英]Bash scripting: Skipping files and grep

Bash scripting is not my strongest point. Bash脚本并不是我的强项。 I have a file structured as 我有一个文件结构为

% comment
filename1 pattern-to-search1
filename1 pattern-to-search2
...

I would like to write a script to grep filename for pattern-to-mat for all for every line in the file. 我想为filename中的每一行编写一个脚本,用于grep filename以实现从pattern-to-mat

So far I have 到目前为止,我有

while read file p
do
    if [ "${file:0:1}" != "%" ]
    then
    grep -o "$p" $file | wc -l
    fi
done
echo -e "\nDone."

But it doesn't skip the files starting with % . 但是它不会跳过以%开头的文件。 Any ideas? 有任何想法吗?

I'd simply do 我只是做

grep -v '^%' | while read file p
do
    grep -c "$p" -- "$file"
done

That way, the comment lines won't even reach the read loop 这样,注释行甚至不会到达 read循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM