过滤掉包含以特定列号开始的文本列的行

过滤掉包含以特定列号开始的文本列的行

我有一个包含柱状数据的文本文件,我想过滤掉满足特定条件的行:倒数第二列中的数据从第 161 列开始,而不是从 162 列开始。因此:

EP10101 12011   SC0     NCI Application Security                                    DLS3270 SC040P20Maintain User Access                                        MF20170901150328000NGS2    20170901150328000            
EP10101 12011   SC0     NCI Application Security                                    DLS3270 SC040P20Maintain User Access                                        PF20170901150328000NGS2    20170901150328000            
OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS20P10RDR Maintenance                                              B20171016171130000NGS2    20171016171130000            
OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS07P10NCFI Lease/Loan Payout Query                                 B20171016134250000NGS2    20171016134250000            
OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS20P10RDR Maintenance                                              B20171016143354000NGS2    20171016143354000

处理后我留下这个:

OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS20P10RDR Maintenance                                              B20171016171130000NGS2    20171016171130000            
OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS07P10NCFI Lease/Loan Payout Query                                 B20171016134250000NGS2    20171016134250000            
OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS20P10RDR Maintenance                                              B20171016143354000NGS2    20171016143354000

如何使用命令行工具实现此目的?

更新

我想删除具有匹配模式的行,并且还需要删除文件中的最后一列。所以我的最终输出应该如下所示。

OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS20P10RDR Maintenance          
OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS07P10NCFI Lease/Loan Payout Query

答案1

有点猜测,因为问题并不完全清楚,但是

sed '/^.\{160\}\S\+/d' file

如果在第 161 列找到“数据”(非空白),则删除该行。所有其他行均被打印。这导致输出:

OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS20P10RDR Maintenance                                              B20171016171130000NGS2    20171016171130000
OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS07P10NCFI Lease/Loan Payout Query                                 B20171016134250000NGS2    20171016134250000
OS10101 12011   DLS     NCI Dealer Systems                                          DLS3270 DLS20P10RDR Maintenance                                              B20171016143354000NGS2    20171016143354000

(忍不住认为有更好的方法,但这就是OP提供的全部。)

答案2

使用下面的命令经测试效果很好

for i in $(awk '{if ((length($1) > "160") && (length($1) < "184"))print NR}' filename); do sed "$i"d p.txt; done

相关内容