Reputation: 11
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?
suppose ps -eaf | grep b
would result into following output:
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
Upvotes: 1
Views: 5388
Reputation: 317
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.
Upvotes: 0
Reputation: 203792
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
Upvotes: 3