由于您没有显示您正在使用的 Input_file,所以我根据您显示的 Input_file 举了我的例子,假设以下是您的 Input_file。
cat Input_file
147 186741 2S74M -162
83 647172 1S75M -221
163 584665 74M2S 271
99 658416 5S65M6S -272
163 718735 60M16S 243
147 186741 2K74M -162
83 647172 1K75M -221
163 584665 74M2K 271
99 658416 5S65M6K -272
163 718735 60M16S 243
下面是我的代码:
awk '(($1==147 || $1==83) && (substr($3,2,1)=="S" || substr($3,3,1)=="S")) || (substr($3,length($3))=="S" && ($1==99 || $1==163)){next} 1' Input_file
现在,当我在 awk 上运行时,我得到的这些值(我添加这些值只是为了检查我的代码是否正常工作)如下。
awk '(($1==147 || $1==83) && (substr($3,2,1)=="S" || substr($3,3,1)=="S")) || (substr($3,length($3))=="S" && ($1==99 || $1==163)){next} 1' Input_file
147 186741 2K74M -162
83 647172 1K75M -221
163 584665 74M2K 271
99 658416 5S65M6K -272
所以你可以看到所有那些不符合你提供的条件的行正在打印,给我一些时间也会在这里添加解释。
编辑:在这里也添加对上述代码的解释,请不要运行它,因为我已将其划分为不同的部分,仅供 OP 理解。
awk '(($1==147 || $1==83)\ ##First condition which re-presents your first awk starts here. checking conditions where $1 value is either 147 OR $1 value is 83
&& \ ## AND
(substr($3,2,1)=="S" \ ##substring of 3rd column is EQUAL to letter S
|| \ ## OR
substr($3,3,1)=="S"))\ ##substring of 3rd column is EQUAL to letter S
|| \ ##OR(means either that first aw condition should be TRUE or this following one), the second major condition for which you used second awk I clubbed both the awks into 2 major conditions here.
(substr($3,length($3))=="S"\##checking if substring of column 3s last letter is EQUAL to S here.
&& \ ## AND
($1==99 || $1==163)){ ##$1 value is either 99 or 163. So if either of above 2 major conditions are TRUE then perform following statements.
next ##next, it is awk keyword which will skip all further statements of line now, without doing any action.
}
1 ##awk works on method of condition and then action, so here I am making condition as TRUE by mentioning as 1 and NO action is mentioned so be default print action will happen which will print current line.
' Input_file ##mentioning Input_file here.