Skip to content
Advertisement

Replacing String Columnwise Using Sed

Given this data

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

I want to replace the string at 2nd column into “” if it is “qux”. Resulting:

34 foo
34 bar
34 
62 foo1
62 
78  

How do you do that with sed? In particular the data is very big with ~10^7 lines

Advertisement

Answer

I wouldn’t actually do it with sed since that’s not the best tool for the job. The awk tool is my tool of choice whenever somebody 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 *$//'

making sure that you use the correct white space (the above uses only spaces).

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement