简体   繁体   中英

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 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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM