awk for loop error when using if else -
here datafile t2, separated tab
_ibaseebna1_1 0.79(0.28-2.22) 0.6540 _ibaseebna1_2 0.88(0.48-1.62) 0.6900 _ibaseebna1_3 0.78(0.32-1.86) 0.5700 ptrend 0.93(0.72-1.20) 0.5800 _ibaseebna1_1 1.85(0.60-5.73) 0.2850 _ibaseebna1_2 1.89(0.57-6.27) 0.2950 _ibaseebna1_3 3.21(1.00-10.33) 0.0510 ptrend 1.39(1.05-1.85) 0.0200 pinteraction 1.39(0.93-2.10) 0.1120
i want remove value of second column if $1==ptrend or ==pinteraction.
the results should like:
_ibaseebna1_1 0.79(0.28-2.22) 0.6540 _ibaseebna1_2 0.88(0.48-1.62) 0.6900 _ibaseebna1_3 0.78(0.32-1.86) 0.5700 ptrend 0.5800 _ibaseebna1_1 1.85(0.60-5.73) 0.2850 _ibaseebna1_2 1.89(0.57-6.27) 0.2950 _ibaseebna1_3 3.21(1.00-10.33) 0.0510 ptrend 0.0200 pinteraction 0.1120
i can use code :
awk -f' ' ' { if ( $1=="ptrend" ) print $1," "," ",$3; else if ( $1=="pinteraction" ) print $1," "," ",$3; else print $0; }' t2.txt > t3.txt
but when try use loop:
for in ptrend pinteraction awk -f' ' -v p=$i '{ if ( $1==p ) print $1," "," ",$3; else print $0; }' t2.txt > t3.txt done
which give results :
_ibaseebna1_1 0.79(0.28-2.22) 0.6540 _ibaseebna1_2 0.88(0.48-1.62) 0.6900 _ibaseebna1_3 0.78(0.32-1.86) 0.5700 ptrend 0.93(0.72-1.20) 0.5800 _ibaseebna1_1 1.85(0.60-5.73) 0.2850 _ibaseebna1_2 1.89(0.57-6.27) 0.2950 _ibaseebna1_3 3.21(1.00-10.33) 0.0510 ptrend 1.39(1.05-1.85) 0.0200 pinteraction 0.1120
anything wrong loop?
the fundamental problem loop is loop. writing shell loop manipulate text wrong approach. can describe in 1 brief, trivial awk command no surrounding shell loop:
awk '!/^_/{$2=""}1' t2
Comments
Post a Comment