内置的shell脚本搜索工具 [英] Built-in search tool in shell scripts

查看:133
本文介绍了内置的shell脚本搜索工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入文件B,含行号参照特定
文件中的A线。我需要使用线路搜索文本模式
在B的数字,例如的B前3行是:

I have an input file "B", containing line numbers referring to specific lines in file "A". I need to search for a text pattern using the line numbers 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 programming and looping with the help of awk etc., but I want to know the built-in tools and options available in Unix (Solaris) for searching, to make my code simple, e.g., if there is a tool or option to search a pattern between 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脚本搜索工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆