问题描述
我有一个输入文件B,含行号参照特定
文件中的A线。我需要使用线路搜索文本模式
在B的数字,例如的B前3行是:
I have an input file "B", containing line numbers referring to specificlines in file "A". I need to search for a text pattern using the linenumbers in "B", e.g. first 3 lines of B will be:
385
527
547
所以,我需要检查模式是386和526之间的present,
528和文件A,在548。我可以用一些复杂的编程做到这一点
和 AWK
的帮助等循环,但我想知道内置
在UNIX(Solaris)获取搜索可用的工具和选项,让我
code简单,例如,如果有工具或选项搜索模式
一个给定的行范围之间,将是有益的。
So I need to check whether the "pattern" is present between 386 and 526,528 and 548 in the file "A". I can do this with some complex programmingand looping with the help of awk
etc., but I want to know the built-intools and options available in Unix (Solaris) for searching, to make mycode simple, e.g., if there is a tool or option to search a patternbetween a given line range it will be useful.
推荐答案
没有循环和复杂性在各种需要awk的匹配(和打印):
No looping or complexity needed for awk to match (and print) in a range:
awk 'NR>start && NR<end && /pattern/'
一个有点棘手文件B的内容转换成正确的 AWK
的条件,但这是:
A bit trickier to convert file B content to the right awk
condition, but this does it:
echo "($(awk 'NR==1{r1=$1;next}
NR>2{printf"||"}
{printf"(NR>%d&&NR<%d)",r1,$1;r1=$1}
END{if(NR<2)printf"0"}' B))&&/yourpattern/"|
awk -f- A
也许稍微简单的将是pre-过滤器使用SED的范围,然后用grep的格局:
Perhaps slightly simpler would be to pre-filter the ranges using sed, and then grep for the pattern:
awk 'NR==1{r1=$1;next}
$1>r1+1{printf"%d,%dp\n",r1+1,$1-1;r1=$1}' B|
sed -nf- A|
grep 'yourpattern'
这篇关于内置的shell脚本搜索工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!