Replace a column with a value conditional on a value in col1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a column with a value conditional on a value in col1
# 1  
Old 11-25-2009
Replace a column with a value conditional on a value in col1

Hi,
Perhaps a rather simple problem...?
I have data that looks like this.
Code:
BPC0013 ANNUL_49610 0 0 1 1
BPC0014 ANNUL_49642 0 0 2 1
BPC0015 ANNUL_49580 0 0 1 1
BPC0016 ANNUL_49596 0 0 2 1
BPC0017 VULGO_49612 0 0 1 1
BPC0018 ANNUL_49628 0 0 1 1
BPC0019 ANNUL_49692 0 0 2 1
170291_HMG ANNUL_170291 0 0 1 1
170351_HMG ANNUL_170351 0 0 1 1
170475_HMG 170475 0 0 2 1
170497_HMG ANNUL_170497 0 0 2 1
170534_HMG ANNUL_170534 0 0 1 1
170559_HMG ANNUL_170559 0 0 1 1

I want to change the col6 value to 2 only if the line does not start with BPC.
Code:
awk -F "\t" '{if($1==BPC) {print $1,$2,$3,$4,$5,$6} else{print $1, $2, $3, $4, $5, $6="2"}} ' testfam.fam > atestedfam.fam

I tried this, but it is changing everything to 2 at col6.

Last edited by genehunter; 11-25-2009 at 01:30 PM..
# 2  
Old 11-25-2009
Quote:
Originally Posted by genehunter
...
Code:
BPC0013 ANNUL_49610 0 0 1 2
BPC0014 ANNUL_49642 0 0 2 2
BPC0015 ANNUL_49580 0 0 1 2
BPC0016 ANNUL_49596 0 0 2 2
BPC0017 VULGO_49612 0 0 1 2
BPC0018 ANNUL_49628 0 0 1 2
BPC0019 ANNUL_49692 0 0 2 2
170291_HMG ANNUL_170291 0 0 1 1
170351_HMG ANNUL_170351 0 0 1 1
170475_HMG 170475 0 0 2 1
170497_HMG ANNUL_170497 0 0 2 1
170534_HMG ANNUL_170534 0 0 1 1
170559_HMG ANNUL_170559 0 0 1 1

I want to change the col6 value to 2 only if the line does not start with BPC.
And the lines that do start with BPC have $6 equal to 2 already.

Quote:
... but it is changing everything to 2 at col6.
So isn't this an expected outcome ?

Anyway -

Code:
$
$ cat f2
BPC0013 ANNUL_49610 0 0 1 2
BPC0014 ANNUL_49642 0 0 2 2
BPC0015 ANNUL_49580 0 0 1 2
BPC0016 ANNUL_49596 0 0 2 2
BPC0017 VULGO_49612 0 0 1 2
BPC0018 ANNUL_49628 0 0 1 2
BPC0019 ANNUL_49692 0 0 2 2
170291_HMG ANNUL_170291 0 0 1 1
170351_HMG ANNUL_170351 0 0 1 1
170475_HMG 170475 0 0 2 1
170497_HMG ANNUL_170497 0 0 2 1
170534_HMG ANNUL_170534 0 0 1 1
170559_HMG ANNUL_170559 0 0 1 1
$
$ awk '{if (!/^BPC/){print $1,$2,$3,$4,$5,"2"} else {print}}' f2
BPC0013 ANNUL_49610 0 0 1 2
BPC0014 ANNUL_49642 0 0 2 2
BPC0015 ANNUL_49580 0 0 1 2
BPC0016 ANNUL_49596 0 0 2 2
BPC0017 VULGO_49612 0 0 1 2
BPC0018 ANNUL_49628 0 0 1 2
BPC0019 ANNUL_49692 0 0 2 2
170291_HMG ANNUL_170291 0 0 1 2
170351_HMG ANNUL_170351 0 0 1 2
170475_HMG 170475 0 0 2 2
170497_HMG ANNUL_170497 0 0 2 2
170534_HMG ANNUL_170534 0 0 1 2
170559_HMG ANNUL_170559 0 0 1 2
$
$

tyler_durden
# 3  
Old 11-25-2009
Hi durden_tyler
Sorry I pasted from the file that already changed :P

Will change the original post now.
# 4  
Old 11-25-2009
Code:
nawk ' /^BPC/{$6 = 2}1' testfam.fam

# 5  
Old 11-25-2009
Thank you all

Thank you durden_tyler & steadyonabix
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional Column Value

Hi Folks, I'm trying tog ain further experience with shell programming and have set my a small goal of writing a little filesystem monitoring script. So far my output is as follows: PACMYDB03 Filesystem Size Used Avail Use% Status /usr/local/mysql/data ... (5 Replies)
Discussion started by: Axleuk
5 Replies

2. Shell Programming and Scripting

Filter on one column and then perform conditional calculations on another column with a Linux script

Hi, I have a file (stats.txt) with columns like in the example below. Destination IP address, timestamp, TCP packet sequence number and packet length. destIP time seqNo packetLength 1.2.3.4 0.01 123 500 1.2.3.5 0.03 44 1500 1.3.2.5 0.08 44 1500 1.2.3.4 0.44... (12 Replies)
Discussion started by: Zooma
12 Replies

3. Shell Programming and Scripting

sed conditional \n replace for each line

How could be removed \n only if appearing at position 80 in the line? (4 Replies)
Discussion started by: RomanF
4 Replies

4. UNIX for Dummies Questions & Answers

If col1 and col2 of any line in both of two files in two files match, col1 and col2

I have two files, and I'm interested in the first two columns of each. File1 compares set1 to set2 (column1 = set1 name, column2 = set2 name). File2 compares set2 to set1 (column1 = set2 name, column2 =set1 name). I want to print the set names (column values) that appear as pairs in both... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. Shell Programming and Scripting

Conditional Search/Replace

I'm looking for an awk or (preferably) sed solution to search a pipe delimited file for any occurrence of an email address that does not include a designed domain, and replace the email address with a blank. E.g. hello|smith@designateddomain.com|jones@anotherdomain.edu|1234| turns into: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

6. Programming

Conditional replace after reading in a file

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (7 Replies)
Discussion started by: naveen@
7 Replies

7. Shell Programming and Scripting

Sed conditional replace

Given this row: |lastname1|middlename1|firstname1|lastname2|middlename2|firstname2 produce this result: |lastname|middlename|firstname where the resultant names are based on the presence of the #2 names above. I.e., if a #2 name is passed (usually will be null,) use that - otherwise... (8 Replies)
Discussion started by: tiggyboo
8 Replies

8. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

9. Shell Programming and Scripting

awk help to do conditional find and replace

Hi, I have a Line input for awk as follows DROP MATERIALIZED VIEW MCR.COMM_STACK; CREATE MATERIALIZED VIEW "MCR"."COMM_STACK" ON PREBUILT TABLE WITHOUT REDUCED PRECISION USING INDEX REFRESH FAST ON DEMAND START WITH sysdate+0 NEXT SYSDATE + 7 WITH PRIMARY KEY USING DEFAULT... (3 Replies)
Discussion started by: rajan_san
3 Replies

10. Shell Programming and Scripting

sed conditional string replace for each line

Hi all, I appreciate the enormous amount of knowledge that flows in this forum. I am an average UNIX user. I have many files with lines like the below. I have separated each line with space for ease of reading. I need to replace the first occurance of "/00" with null on those lines that have... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies
Login or Register to Ask a Question