awk删除以符号开头但没有关键字的行

时间:2018-05-15 12:35:26

标签: awk

我正在尝试有选择地删除以array2开头但不包含关键字<?php foreach($array1 as $index => $val) { foreach($array2 as $index2 => $val2){?> //here i want to compare $val with $val2 to check $val2 is not empty or not <?php } }?> Globals.ThisAddIn.Application.ActiveCell.Formula = "=zAirAtmTPFC()"; Globals.ThisAddIn.Application.Dialogs[Excel.XlBuiltInDialog.xlDialogFunctionWizard].Show(); 的行。不以#开头的行未更改。我可以使用第一个Build删除以Type开头的所有行,但不确定如何有选择地删除以#开头但不包含关键字的行。第二个#执行但只留下两行(#CN过滤器:                                    #Flags = 1,2,3)。谢谢你:)。

AWK

awk

AWK

#

输入 awk

awk '!/#/' input < out  # will remove all lines with #

所需的输出

awk '/#/ && !/Build|Length/' input < out  # remove lines starting with # but must not have Build or Length in them

5 个答案:

答案 0 :(得分:2)

您希望对以#开头并且不包含BuildType的行执行某些操作,对吧?我相信你可以写下这个条件:

  1. # = /^#/
  2. 开始
  3. AND = &&
  4. 不包含BuildType = !/Build|Type/
  5. /^#/ && !/Build|Type/
    

    现在,当条件成真时你想做什么?不打印当前行。所以你可以简单地写一下:

    awk '/^#/ && !/Build|Type/{next} 1'
    

    但如果您更喜欢使用awks默认打印给出真实条件,那么您只需要否定您的条件(a{next} 1 = !a):

    awk '!(/^#/ && !/Build|Type/)'
    

    通过布尔代数(!(a && b) = !a || !b)可以简化为:

    awk '!/^#/ || /Build|Type/'
    

答案 1 :(得分:1)

$ awk '!/^#/ || /Build|Type/' file
#Build = NCBI Build 37
#  Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy

如果要删除这些初始#字符及其后面的空格:

$ awk '!/^#/ || /Build|Type/ { sub("^#[[:blank:]]*", ""); print }' file
Build = NCBI Build 37
Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy

答案 2 :(得分:1)

关注awk也可以帮助您。

awk '!(/^#/ && !/Build/ && !/Type/){gsub(/^#|^# +/,"");print}'  Input_file

<强> 说明:

awk '
!(/^#/ && !/Build/ && !/Type/){  ##Checking condition here if a line starts with # and NOT having string Build and Type in it, Negating this condition to work it as opposite, if this condition is TRUE then do following.
  gsub(/^#|^# +/,"");            ##Using gsub to remove hash in starting of a line OR remove a hash starting fr a line with space with NULL in current line.
  print                          ##Printing the current line here.
}' Input_file                    ##Mentioning the Input_file name here.

答案 3 :(得分:1)

sed解决方案:

insert into protector values('101','Dragon');
insert into protector values('102','Lion');

insert into master values('201','Fairy');

insert into player values('01','Joe','101','');
insert into player values('02','Elsa','102','201');

答案 4 :(得分:0)

awk '!/CN|Fl/{sub(/\43/,"")sub(/^\s*/,"");print}' file

Build = NCBI Build 37
Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy