Reversing a line based on column

Hi,

I have a file that looks like this (tab seperated):

read   -  DFHJ
read1 -  IOPE
read2 + AAAB
read3 + MMMN

Basically what i want to do is reverse column 3 if column 2 has a - but leave it if its +

so the output would look like this:

read   -  JHFD
read1 -  EPOI
read2 + AAAB
read3 + MMMN

I am able to use awk to reverse individual lines but I cannot do it according to a particular column. thanks

:smiley:

 awk 'function rev(str){ tmp=""; for(i=length(str);i>0;i--)
                                    {tmp=tmp substr(str,i,1)} 
                                 return tmp}
      {print $1, $2, ($2=="-") ? rev($3) : $3}  '  filename > newfilename

With Perl:

perl -ape'
  s/(-\s*)(\w+)/$1.reverse$2/e
  ' infile

nice function, especially this line:

tmp=tmp substr(str,i,1)