【问题标题】:Grep pattern and select portion of a after a matching patternGrep 模式并在匹配模式之后选择 a 的一部分
【发布时间】:2018-10-11 05:56:03
【问题描述】:

Grep 模式并选择匹配模式 41572: 90000: 和 90002: 之后的部分行

输入

hyt : generation
str : 122344
stks : 9000233
dhy : 9000aaaa
sjyt : hist : hhh9000kkk
Count ch : 41572:47149-47999/2(14485-14910) 41584:47149-47999/2(14911-15449) 90000:47919-47999/2(15447-15477) 90002:47919-47999/2(15478-15418) 
drx : 12345

这里使用的代码

awk '
{
  flag=""
  for(i=1;i<=NF;i++){
    if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){
       flag=1
       printf("%s%s",$i,i==NF?ORS:OFS)
    }
  }
}
!flag
'   Input_file

使用 RavinderSingh13 先生的上述代码,我得到以下输出

hyt : generation
str : 122344
stks : 9000233
dhy : 9000aaaa
sjyt : hist : hhh9000kkk
41572:47149-47999/2(14485-14910) 90000:47919-47999/2(15447-15477) 90002:47919-47999/2(15478-15418) 
drx : 12345

我需要以下输出

hyt : generation
str : 122344
stks : 9000233
dhy : 9000aaaa
sjyt : hist : hhh9000kkk
Count ch : 41572:47149-47999/2(14485-14910) 90000:47919-47999/2(15447-15477) 90002:47919-47999/2(15478-15418) 
drx : 12345

提前致谢

【问题讨论】:

  • 不清楚,您能否在问题中更清楚一点,预期输出背后的逻辑是什么。

标签: awk


【解决方案1】:

编辑:根据 OP 的新问题添加解决方案。

awk '{flag="";for(i=1;i<=NF;i++){if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){flag=1;printf("%s%s",$i,i==NF?ORS:OFS)}}} !flag'

awk '
{
  flag=""
  for(i=1;i<=NF;i++){
    if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){
       flag=1
       printf("%s%s",$i,i==NF?ORS:OFS)
    }
  }
}
!flag
'   Input_file


您能否尝试以下操作(尽管仅按照显示的示例输出不完全清楚)。

awk 'NF>1{for(i=1;i<=NF;i++){if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){printf("%s%s",$i,i==NF?ORS:OFS)}};next} 1' Input_file

现在也添加非单线形式的解决方案。

awk '
NF>1{
  for(i=1;i<=NF;i++){
    if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){
      printf("%s%s",$i,i==NF?ORS:OFS)
    }
  }
  next
}
1
'  Input_file

说明:这里也为上述代码添加说明。

awk '
NF>1{                                                    ##Checking if NF is greater than 1.
  for(i=1;i<=NF;i++){                                    ##Using for loop to go through from value 1 to till value of NF.
    if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){    ##Checking if value of fields is either 41572 OR 90000 OR 90002 then do following.
      printf("%s%s",$i,i==NF?ORS:OFS)                    ##Print the field value in case above condition is TRUE with NEW line if i==NF or space if not.
    }
  }
  next                                                   ##Next will skip all further statements from here.
}
1                                                        ##1 will print all edited/non-edited lines here.
' Input_file                                             ##Mentioning Input_file name here.

【讨论】:

  • RavinderSingh13,是的,这正是我需要得到的输出......很难解释逻辑......
  • @OXXO,写解释会在几分钟内添加。
  • @OXXO,请立即查看,如有任何疑问,请告诉我。
  • RavinderSingh13,很多代码都非常适合第一个输入示例。您是否有机会添加或修改代码以使用 Example2 输入并获得所需的输出,
  • @OXXO,现在请检查我的编辑。
猜你喜欢
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多