我需要从文件中的列子集中查找字符串,而不是从整个文件

时间:2016-03-23 14:52:07

标签: linux shell grep

我正在使用LINUX,我想使用shell实现这一点。

我想提取字符串" db2"来自字段以粗体显示,这对应于"标记"字段,此字段由/ tab分隔,我不能使用固定位置,因为它的大小可变。

实际文件摘录为:显示2条记录,以时间戳开头。

[杰夫Y:格式化数据]

create_ts   id  tags    title   body    answers

2011-01-03T20:52:52.880 5   **nosql^&^rdbms^&^database-recommendation** what are the differences between nosql and a traditional rdbms  what are the differences between nosql and a traditional rdbms   over the last few months  nosql has been frequently mentioned in the technical news  what are its most significant features relative to a traditional rdbms  at what level  physical  logical  do the differences occur   where are the best places to use nosql  why      nosql is a kind of database that doesn t have a fixed schema like a traditional rdbms does   with the nosql databases the schema is defined by the developer at run time   they don t write normal sql statements against the database  but instead use an 
2011-01-04T14:26:06.730 162 sql^&^explain-plan^&^db2    what does hsjoin mean in an explain plan    i have the following explain plan results from a query on my db2 database   0 select statement   estimated costs    5 928e 02  timerons           1 return            2 hsjoin                3  o  fetch ltbp                    4 ixscan ltbp m  key columns   0                5  i  fetch ltbk                    6 ixscan ltbk v  key columns   0   what does the hsjoin on line 2 mean    it s a hash join  
2011-01-07T16:44:52.210 394 **database-recommendation^&^feature comparison^&^permissions**  which database engines will allow me to grant revoke on a specific column   if i have a table with a single column of sensitive data  and i want to grant broad use of the table without exposing that one column  i know that i can create a view that gives them access to all the non sensitive columns  however  postgresql allows you to grant column level permissions in the form of  grant select  col1     coln  on table to role    are there other engines which provide this capability     sql server 2000  2005  2008 has this capability  grant   all   privileges              permission     column       n             n            on   class      securable   to 

2 个答案:

答案 0 :(得分:0)

awk救援! 如果您没有标记来根据字符数分隔部分,则可以限制模式匹配。

$ awk 'substr($0,start,length)~/pattern/' file

例如,如果感兴趣的部分以14和9个字符长开始,则搜索模式abc。

$ awk 'substr($0,14,9)~/abc/' file

如果数据是分隔的,例如使用标签,并且您希望将搜索范围限制为字段2,则可以执行此操作

$ awk -F'\t' '$2~/abc/' file

当然,这些只是猜测,直到你有一个具体的例子来证明真正的问题并验证脚本。

答案 1 :(得分:0)

您的示例数据未显示制表符分隔符,但假设所有字段实际上都是制表符分隔的:

使用awk,您需要拆分标记分隔符上的第3个字段,并在结果中查找您的标记:

awk -F"\t" '{split($3,a,/\^&\^/);for(t in a)if(a[t]=="db2"){print;next}}'