AWK或SED在使用grep时从每一行删除模式

时间:2022-03-20 01:47:54

i have grep 2 columns , suppose col1 and col2. In col2 i want to remove a pattern which occurs in every line. how to use awk/sed for this purpose?

我有grep 2列,假设col1和col2。在col2中,我想删除每行中出现的模式。如何使用awk / sed达到此目的?

suppose ps -eaf | grep b would result into following output:

假设ps -eaf | grep b将导致以下输出:

col1 col2       col3
1    a/b/rac     123
2    a/b/rac1    456
3    a/b/rac3    789

I want output to get stored in a file like this :

我希望输出存储在这样的文件中:

1 rac
2 rac1
3 rac3

2 个解决方案

#1


2  

Vaguely speaking, this might do what you want:

含糊地说,这可能会做你想要的:

$ awk 'sub(/.*b\//,"",$2){print $1, $2}' file 
1 rac
2 rac1
3 rac3

assuming file contains:

假设文件包含:

col1 col2       col3
1    a/b/rac     123
2    a/b/rac1    456
3    a/b/rac3    789

#2


0  

You will want to pipe the output to

您将要输出管道

| awk '{sub(/^.*\//, "", $2); print $1, $2}'

The first command in the awk program changes the contents of the second field to only contain whatever was after the final / character, which appears to be what you want. The second command prints the first two fields.

awk程序中的第一个命令将第二个字段的内容更改为仅包含最终/字符之后的内容,这似乎是您想要的。第二个命令打印前两个字段。

#1


2  

Vaguely speaking, this might do what you want:

含糊地说,这可能会做你想要的:

$ awk 'sub(/.*b\//,"",$2){print $1, $2}' file 
1 rac
2 rac1
3 rac3

assuming file contains:

假设文件包含:

col1 col2       col3
1    a/b/rac     123
2    a/b/rac1    456
3    a/b/rac3    789

#2


0  

You will want to pipe the output to

您将要输出管道

| awk '{sub(/^.*\//, "", $2); print $1, $2}'

The first command in the awk program changes the contents of the second field to only contain whatever was after the final / character, which appears to be what you want. The second command prints the first two fields.

awk程序中的第一个命令将第二个字段的内容更改为仅包含最终/字符之后的内容,这似乎是您想要的。第二个命令打印前两个字段。