Shahin
Shahin

Reputation: 1316

apply a command to the values of a column in a text file and replace

cat file

1   a   GYPA    
2   b   GYPA    002099.7:c.59
2   c   GYPA    002099.7:c.71
2   d   GYPA    002099.7:c.72
3   e   GYPA    002099.7:c.59
4   f   GYPA    002099.7:c.140
5   g   GYPA    002099.7:c.68

I have the following file I am processing. I would like to replace the fourth column with the output of command <options> $4 as follows:

EXPECTED OUTPUT

1   a   GYPA    
2   b   GYPA    out1
2   c   GYPA    out2
2   d   GYPA    out3
3   e   GYPA    out4
4   f   GYPA    out5
5   g   GYPA    out6

where out1 is cmd <opts> '002099.7:c.59'

This gets very complicated with awk since I need to use quotes '002099.7:c.59' to wrap the text.

I have been using:

t="\t"
while read -r a b c d
    echo -e $a$t$b$t$c$t$(cmd opt $d);
done

When the 4th column entry is empty (i.e. first line), I get an error output from cmd <opts>. Is there a simpler alternative to this where I could use column number besides awk?

Upvotes: 0

Views: 61

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 296049

while IFS=$'\t' read -r col1 col2 col3 col4; do
  if [ -n "$col4" ]; then
    col4=$(yourcommand "$col4")
    printf '%s\t%s\t%s\t%s\n' "$col1" "$col2" "$col3" "$col4"
  else
    printf '%s\t%s\t%s\n'     "$col1" "$col2" "$col3"
  fi
done

Upvotes: 1

Related Questions