在'grep'结果中包含标题 [英] Include header in the 'grep' result
本文介绍了在'grep'结果中包含标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法将'head -1'和'grep'命令合并为一个目录中的所有文件,并将输出重定向到输出文件。我可以使用'sed'来做到这一点,但它似乎没有grep那么快。
Is there a way to combine 'head -1' and 'grep' command into one for all the files in a directory and redirect the output to an output file. I can do this using 'sed' but it seems that it is not as fast as grep.
sed -n '1p;/6330162/p' infile*.txt > outfile.txt
使用grep我可以一次完成以下一个文件:
Using grep I can do the following one file at a time:
head -1 infile1.txt; grep -i '6330162' infile1.txt > outfile.txt
但是,我需要为目录中的所有文件执行此操作。插入通配符并没有什么帮助,因为它首先打印标题,然后是grep输出。
However, I need to do it for all files in the directory. Inserting a wildcard is not helping as it is printing headers first and then the grep output.
推荐答案
for file in *
do
[ "$file" = outfile.txt ] && continue
head -n 1 "$file"
grep -i '...' "$file"
done > outfile.txt
这篇关于在'grep'结果中包含标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文