Rachita Wadera
Rachita Wadera

Reputation: 11

AWK or SED to remove pattern from every line while using grep

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

Answers (2)

wyrm
wyrm

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

Ed Morton
Ed Morton

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

Related Questions