Help with replace column data with specific word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with replace column data with specific word
# 1  
Old 04-23-2012
Help with replace column data with specific word

Input file:
Code:
Populus_trichocarpa	30	0	50	0	0 US
Vitis_vinifera	1	18	2	8	6 US
Populus_trichocarpa	1	5	100	0	0 US
Arabidopsis_lyrata_subsp._lyrata	0	90	0	0	0 US
Glycine_max	0	2	3	0	70 UK

Desired output file:
Code:
Populus_trichocarpa	YES	NO	YES	NO	NO US
Vitis_vinifera	YES	YES	YES	YES	YES US
Populus_trichocarpa	YES	YES	YES	NO	NO US
Arabidopsis_lyrata_subsp._lyrata	NO	YES	NO	NO	NO US
Glycine_max	NO	YES	YES	NO	YES UK

If the column 2-6, any column shown equal to 0, replace it with "NO"
If the column 2-6, any column shown except 0, replace it with "YES"

Thanks for any advice.

Last edited by perl_beginner; 04-23-2012 at 02:35 AM..
# 2  
Old 04-23-2012
Hi

Code:
awk '{for(i=2;i<=NF;i++)$i=$i==0?"NO":"Yes";}1' file

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 04-23-2012
Hi Guru,

I just edited my question.
Do you have any idea if just focus on the data that in column 2-6?
Because some of my input file, column 7 also got some data which I just need to print out them.
I only interested to replace the data in column 2-6 into "YES" or "NO" if fully requirement.
Many thanks.
# 4  
Old 04-23-2012
Hi, have a look at the for statement. On every line it is being executed so that every field starting from field 2 ( i=2 ) is processed. In the suggestion provided by guruprasadr it currently stops at the last field ( i<=NF ) . What do you think would make it stop processing at the field before the last field?
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-23-2012
Thanks for reminding Smilie
The below command worked fine:
Code:
awk '{for(i=2;i<=NF;i++)$i=$i==0?"NO":"Yes";}1'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find Word in Column with replace (2)

Dear ALL, I have sample file : 4c:66:41:6b:b5:5f, 00:00,00:00, -22:-25, robert, 10.101.3.119, host1 4c:66:41:2c:c1:5a, 00:00,00:00, -21:-25, 10.101.3.112, host1 4c:66:41:6b:b1:5a, 00:00,00:00, -21:-25, Julia, 10.101.3.113, host1 4c:66:41:2c:c1:5b, 00:00,00:00, -21:-25, 10.101.3.115, host1... (2 Replies)
Discussion started by: gnulyn
2 Replies

2. Shell Programming and Scripting

Find Word in Column with replace

Hi ALL i have file.txt with text : 4c:66:41:6b:b5:5f, 00:00,00:00, -22:-25, users1, 10.101.3.119, host1 4c:66:41:2c:c1:5a, 00:00,00:00, -21:-25, 10.101.3.112, host1 4c:66:41:6b:b1:5e, 00:00,00:00, -21:-25, users1, 10.101.3.113, host1 4c:66:41:2c:c1:5b, 00:00,00:00, -21:-25, 10.101.3.115,... (3 Replies)
Discussion started by: gnulyn
3 Replies

3. Shell Programming and Scripting

Replace a specific column with a specific value

Hi, I am looking to replacing value of a specific column of /etc/pam.d/system-auth file. My file looks like this password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok expected result password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok... (5 Replies)
Discussion started by: Litu1988
5 Replies

4. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

5. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

6. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

7. Shell Programming and Scripting

Replace data of one column with data on other file corresponding to transaction ID matched

Hi All, I have two files one of which having some mobile numbers and corresponding value whose sample content as follows: 9058629605,8.0 9122828964,30.0 And in second file complete details of all mobile numbers and sample content as follows and delimeter used is comma(,): ... (8 Replies)
Discussion started by: poweroflinux
8 Replies

8. Shell Programming and Scripting

Remove specific word from data contents problem asking

Hi, Below is my input file: >tempZ_1 SAFSDAFDSG GGERRTTZZZ ZASRARARET WETPOASDAZ ZZZASASFAS >temp_9 ASAEGPIOEO EIOPIZZZAS FDFGZZZARA ESEAZZZAAS . . Desired output file: (9 Replies)
Discussion started by: patrick87
9 Replies

9. Shell Programming and Scripting

How to cut first line only from a text near a specific column without cutting a word

First I have to say thank you to this community and this forum. You helped me very much builing several useful scripts. Now, I can't get a solution the following problem, I'm stuck somehow. Maybe someone has an idea. In short, I dump a site via lynx and pipe the output in a file. I need to... (7 Replies)
Discussion started by: lowmaster
7 Replies

10. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 Replies
Login or Register to Ask a Question