How to replace column values if equal specific value to specific character and if not equal these value replace it by another character in Linux ?
0
0
Entering edit mode
16 months ago
Ahmad ▴ 10

Hi everyone

I have file with 3 column, I need check if the 3rd column < 10 replace value by character T but if not < 10 replace value by character S

The file like

T227 1   10.736439
T228 1    5.690412
T229 1    8.534977
T230 1   10.711639
T231 1    6.298395

Thank you for your help me

linux awk • 1.8k views
ADD COMMENT
0
Entering edit mode

What was your approach to solving this? Where did you have a problem?

ADD REPLY
0
Entering edit mode

I need replace 3rd column values with specific character if it <10 but if it not replace it with another character in huge file

ADD REPLY
0
Entering edit mode

I got that. What did you do to try to solve it?

ADD REPLY
0
Entering edit mode

I used awk

awk '{if($3 <8) print $1,$2,$3 = "c"}' file_name

but I can't refer to another values

ADD REPLY
2
Entering edit mode

Are you trying to replace the third column? Maybe you can try an if-else construct? There are some examples here https://www.thegeekstuff.com/2010/02/awk-conditional-statements/

ADD REPLY
0
Entering edit mode

Thank you barslmn

I used this command

awk '{if ($3 <10)print $1,$2,$3="c"; else print $1,$2,$3="s"}' file_name
ADD REPLY
0
Entering edit mode

You can do what you've said twice, using an intermediate file to save the results. Code not checked, just adapted your example:

awk '{if($3 <10) print $1,$2,$3 = "c"}' input.tsv > output1.tsv
awk '{if($3 >=10) print $1,$2,$3 = "s"}' output1.tsv > output2.tsv
ADD REPLY

Login before adding your answer.

Traffic: 2711 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6