-3

I have a tab-separated text, within the text is a string that appears on several occasions and I need to remove it when accompanied by other strings.

A       B      C        D   E   F   G   H   I   J
string  string string   car29   ""  ""  ""  ""  ""  string # We don't do anything
string  string string   pool    car29   ""  ""  ""  ""  string # Remove car29
string  string string   fifo    pool    car29   ""  ""  ""  string # Remove car29
string  string string   car29   ""  ""  ""  ""  ""  string # We don't do anything

The key word is car29 and we just act on rows where this word appears

Expected Output

A       B      C        D   E   F   G   H   I   J
string  string string   car29   ""  ""  ""  ""  ""  string 
string  string string   pool    ""  ""  ""  ""  ""  string 
string  string string   fifo    pool    ""  ""  ""  ""  string 
string  string string   car29   ""  ""  ""  ""  ""  string 
Polucho
  • 23
  • 5

2 Answers2

0

Perl to the rescue!

perl -F'/\t/' -ape 's/(^|(?<=\t))car29($|(?=\t))/""/g if grep $_ ne "car29" && $_ ne q(""), @F[3..8]'
  • -F sets the column separator
  • -a splits to colunns (into the @F array)
  • -p processes input line by line, printing the results
  • the substitution happens only if the column 3 .. 8 (just guessing, your specification is vague) isn't empty and is different to car29
  • the substitution replaces car29 by "" when it's preceded by the beginning of line or a tab, and followed by the end of line or a tab. Look around assertions are used for cases where there are several car29s neighbouring. The ^ and $ are probably not needed, as the first three columns and the last one in your examples never contain car29 (incomplete spec again).
choroba
  • 231,213
  • 25
  • 204
  • 289
0

With sed

sed '/\(.*"".*\)\{5\}/! s/car29/""/' <<\eof
A       B      C        D   E   F   G   H   I   J
string  string string   car29   ""  ""  ""  ""  ""  string # We don't do anything
string  string string   pool    car29   ""  ""  ""  ""  string # Remove car29
string  string string   fifo    pool    car29   ""  ""  ""  string # Remove car29
string  string string   car29   ""  ""  ""  ""  ""  string # We don't do anything
eof

A       B      C        D   E   F   G   H   I   J
string  string string   car29   ""  ""  ""  ""  ""  string # We don't do anything
string  string string   pool    ""   ""  ""  ""  ""  string # Remove car29
string  string string   fifo    pool    ""   ""  ""  ""  string # Remove car29
string  string string   car29   ""  ""  ""  ""  ""  string # We don't do anything
Costas
  • 343
  • 1
  • 6