如何排除匹配的行及其前面的行?

时间:2018-12-21 02:13:43

标签: shell grep

在我的项目中,我想排除a.txt中的某些内容。

a.txt中的内容是:

3085180 nscc135 PEND  intelg_sma gcn01                   *_D4d_5.sh Dec 14 14:32
The user has reached his/her job slot limit;
3085182 nscc135 PEND  intelg_sma gcn01                   *_C3v_5.sh Dec 14 14:32
The user has reached his/her job slot limit;
3085184 nscc135 PEND  intelg_sma gcn01                   *_C3v_5.sh Dec 14 14:33
The user has reached his/her job slot limit;
3085186 nscc135 PEND  intelg_sma gcn01                   *_D4d_5.sh Dec 14 14:33
The user has reached his/her job slot limit;
3095798 nsgg289 PEND  intelg_sma gcn03                   400./      Dec 19 09:51
One of the user's groups has reached its job slot limit;
3096822 nsgg289 PEND  intelg_sma gcn03                   460./      Dec 19 18:00
One of the user's groups has reached its job slot limit;
3098784 nsgg289 PEND  intelg_sma gcn04                   xhzha      Dec 20 14:48
One of the user's groups has reached its job slot limit;
3099298 nsgg276 PEND  intelg_sma gcn01                   Pd3.2      Dec 20 18:11
The user has reached his/her job slot limit;
3099299 nsgg276 PEND  intelg_sma gcn01                   Pd2.8      Dec 20 18:12
The user has reached his/her job slot limit;
3099311 nsgg276 PEND  intelg_sma gcn01                   Pt1.8      Dec 20 18:40
The user has reached his/her job slot limit;
3099312 nsgg276 PEND  intelg_sma gcn01                   Pt2        Dec 20 18:41
The user has reached his/her job slot limit;

我要排除包含The user内容和上一行的这一行。 我尝试过:

grep -A 1 -v "The\ user" a.txt

grep -AB 1 -v "The\ user" a.txt

但是他们都失败了。

正确的结果应该是:

3095798 nsgg289 PEND  intelg_sma gcn03                   400./      Dec 19 09:51
One of the user's groups has reached its job slot limit;
3096822 nsgg289 PEND  intelg_sma gcn03                   460./      Dec 19 18:00
One of the user's groups has reached its job slot limit;
3098784 nsgg289 PEND  intelg_sma gcn04                   xhzha      Dec 20 14:48
One of the user's groups has reached its job slot limit;

2 个答案:

答案 0 :(得分:1)

我不认为-A-B会像您期望的那样工作。它不会“也排除”上一行或下一行。因此,我不认为仅使用-v就能解决您的问题。

一种方法是获取包含您的模式的行的行号,以及紧接在它们之前的行。然后,使用grep删除这些行号:

sed

结果:

grep -n -B 1 'The user' a.txt | egrep -v '^-' | sed -r 's/^([0-9]+).*/\1d/' | sed -f - a.txt

解释:

$ grep -n -B 1 'The user' a.txt | egrep -v '^-' | sed -r 's/^([0-9]+).*/\1d/' | sed -f - a.txt
3095798 nsgg289 PEND  intelg_sma gcn03                   400./      Dec 19 09:51
One of the user's groups has reached its job slot limit;
3096822 nsgg289 PEND  intelg_sma gcn03                   460./      Dec 19 18:00
One of the user's groups has reached its job slot limit;
3098784 nsgg289 PEND  intelg_sma gcn04                   xhzha      Dec 20 14:48
One of the user's groups has reached its job slot limit;

答案 1 :(得分:1)

它也仅使用sed即可起作用:

sed -e 'N;s/\n/####/;/The user/d' -e 's/####/\n/g' a.txt

结果:

3095798 nsgg289 PEND  intelg_sma gcn03                   400./      Dec 19 09:51
One of the user's groups has reached its job slot limit;
3096822 nsgg289 PEND  intelg_sma gcn03                   460./      Dec 19 18:00
One of the user's groups has reached its job slot limit;
3098784 nsgg289 PEND  intelg_sma gcn04                   xhzha      Dec 20 14:48
One of the user's groups has reached its job slot limit;

首先,使用N;s/\n/####/;/The user/d####合并两行,并与The user删除行;然后调用s/####/\n/g将匹配的行拆分为两行。