Replacing String Columnwise Using Sed

26,729

Solution 1

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

Solution 2

No trailing spaces:

sed 's/qux$//' < file

If it must be in the second column (of potentially more than three columns):

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

(Note that there is a literal tab and space; sed doesn't match tabs with '\t';

But awk is better for tabular data:

awk '{ if ($2 == "qux") {$2 = ""; print} else { print }; }' < file

Solution 3

 nawk '{ gsub( /qux/, " " ); print }' filename
Share:
26,729
Danf
Author by

Danf

Updated on July 30, 2020

Comments

  • Danf
    Danf almost 4 years

    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