提取其列仅包含特定模式的行

时间:2016-05-30 11:40:35

标签: regex file unix awk grep

我有一个包含一些行的文件 这些行看起来像这样:

zadde205554800156569001
sfsdf848532300258112668
fsdfs552258800556220056
.
.
.
dzaed686745800158748048 

我希望从第13列开始只使用一些awk和正则表达式来获取包含001005的行

所以输出应该是这样的:

zadde205554800156569001
fsdfs552258800556220056     
dzaed686745800158748048

我尝试了一些grep命令,但徒劳无功

2 个答案:

答案 0 :(得分:1)

描述

^.{12}(?:001|005)

Regular expression visualization

此正则表达式将执行以下操作:

  • 允许字符串以任意12个字符开头
  • 要求字符串在第13个字符处包含子字符串001005

实施例

现场演示

https://regex101.com/r/lQ2oD6/1

示例文字

zadde205554800156569001
sfsdf848532300258112668
fsdfs552258800556220056
.
.
.
dzaed686745800158748048 

样本匹配

zadde205554800156569001
fsdfs552258800556220056
dzaed686745800158748048 

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  .{12}                    any character except \n (12 times)
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    001                      '001'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    005                      '005'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------

答案 1 :(得分:0)

$ awk '/.{12}00[15]/' file
zadde205554800156569001
fsdfs552258800556220056
dzaed686745800158748048

$ grep -E '.{12}00[15]' file
zadde205554800156569001
fsdfs552258800556220056
dzaed686745800158748048

$ sed -E -n '/.{12}00[15]/p' file
zadde205554800156569001
fsdfs552258800556220056
dzaed686745800158748048