Replacing row by column with sed

Given this data

34 foo
34 bar
34 qux
62 foo1
62 qux
78 qux 

I want to replace the row with the second column with "if it is" qux ". Result:

34 foo
34 bar
34 
62 foo1
62 
78  

How do you do this with sed? In particular, the data is very large with ~ 10 ^ 7 rows

+3
source share
3 answers

I would not really do this with sed, as this is not the best tool to work with. A tool awkis my selection tool when someone mentions columns.

cat file | awk '$2 == "qux" { print $1 } $2 != "qux" { print $0 }'

or the simplest form:

cat file | awk '{ if ($2 == "qux") {$2 = ""}; print }'

If you must use sed:

cat file | sed 's/  *qux *$//'

make sure you use the correct empty space (in the example above only spaces are used).

+15
source

:

sed 's/qux$//' < file

( ):

sed 's/\([0-9][  ]*\)qux\(.*\)/\1\2/'

( , ; sed '\ t';

awk :

awk '{ if ($2 == "qux") {$2 = ""; print} else { print }; }' < file
+3
 nawk '{ gsub( /qux/, " " ); print }' filename
+1
source

Source: https://habr.com/ru/post/1705762/


All Articles