awk to change contents of field based on condition in same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to change contents of field based on condition in same file
# 1  
Old 01-27-2018
awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output without changing file to the desired output. Thank you Smilie.

file tab-delimeted
Code:
R_Index	Chr	Start	Func.refGene	Gene.refGene	GeneDetail.refGene	ExonicFunc.refGene	AAChange.refGene
1	chr1	17313157	splicing	ATP13A2	NM_001141973:exon28:c.3221-30C>T:p=?;NM_022089:exon28:c.3236-30C>T:p=?	.	.
2	chr1	17313157	exonic	ATP13A2	.	nonsynonymous SNV	ATP13A2:NM_001141974:exon27:c.3214G>A:p.Ala1072Thr
3	chr1	17313165	splicing	ATP13A2	NM_001141973:exon28:c.3221-30C>T:p=?	.	.

awk
Code:
awk 'BEGIN {OFS=FS="\t"} {if ($8==".")sub($8,$6); print }' file

desired output tab-delimeted
Code:
R_Index	Chr	Start	Func.refGene	Gene.refGene	GeneDetail.refGene	ExonicFunc.refGene	AAChange.refGene
1	chr1	17313157	splicing	ATP13A2	NM_001141973:exon28:c.3221-30C>T:p=?;NM_022089:exon28:c.3236-30C>T:p=?	.	NM_001141973:exon28:c.3221-30C>T:p=?;NM_022089:exon28:c.3236-30C>T:p=?
2	chr1	17313157	exonic	ATP13A2	.	nonsynonymous SNV	ATP13A2:NM_001141974:exon27:c.3214G>A:p.Ala1072Thr
3	chr1	17313165	splicing	ATP13A2	NM_001141973:exon28:c.3221-30C>T:p=?	.	NM_001141973:exon28:c.3221-30C>T:p=?

# 2  
Old 01-27-2018
Code:
awk 'BEGIN {OFS=FS="\t"} {if ($8==".") $8=$6; print }' file

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 01-27-2018
In addition to what rdrtx1 already suggested, we could try something a little closer to what you were doing with sub():
Code:
awk 'BEGIN {OFS=FS="\t"} {sub(/^\.$/,$6,$8); print}' file

but this will only work if $6 does not expand to a string containing any ampersand (&) characters. (I.e., it works with your sample data, but is dangerous if you don't know what characters might appear in field 6 in data your script might process later.) One could also try the slightly simpler:
Code:
awk 'BEGIN {OFS=FS="\t"} $8=="." {$8=$6} 1' file

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-29-2018
Thank you both very much Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. UNIX for Beginners Questions & Answers

Change the field color based on condition in email

Request your help to change the field color based on condition , if it is otherthan 0. using html in unix. Here is my condition for(i=1;i<=NF;i++) { print "<td> "$i"</td> } Please use CODE tags when displaying sample input, output, and code segments. (17 Replies)
Discussion started by: CatchMe
17 Replies

3. Shell Programming and Scripting

awk to update value in field of out file using contents of another Ask

In the out.txt below I am trying to use awk to update the contents of $9.. If $9 contains a + or - then $8 of out.txt is used as a key to lookup in $2 of file. When a match ( there will always be one) is found the $3 value of that file is used to update $9 of out.txt separated by a :. So the... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

How can I change file value based on condition

Hi, Gurus, I got a problem to resolve following issue: I have one file file1as following: start_dt=2010-01-01 12:00:02 start_dt=2011-01-01 09:00:02 start_dt=2009-01-01 11:00:02I have another file file2 as following: title1, 2010-01-03 10:00:02 title2, 2011-01-04 11:00:02 title3,... (5 Replies)
Discussion started by: ken002
5 Replies

6. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

7. Shell Programming and Scripting

Update a field in a file based on condition

Hi i am new to scripting. i have a file file.dat with content as : CONTENT_STORAGE PERCENTAGE FLAG: /storage_01 64% 0 /storage_02 17% 1 I need to update the value of FLAG for a particular CONTENT_STORAGE value I have written the following code #!/bin/sh threshold=20... (1 Reply)
Discussion started by: kichu
1 Replies

8. Shell Programming and Scripting

Change in Input feed based on condition file

Sorry Guys for not being able to explain in one of my earlier post. I am now putting my requirement with the input file and desired output file. In the below input file - Transaction code is at position 31:40. Business code is from position 318:321 TSCM00000005837 ... (7 Replies)
Discussion started by: varunrbs
7 Replies

9. Shell Programming and Scripting

awk script to split a file based on the condition

I have the file with the records like 4234234 US phone 3244234 US cup 2342342 CA phone 8947234 US phone 2389472 CA cup 2348972 US maps 3894234 CA phone I want the records with (US,phone) as record to be in one file, (Us, cup) in another file and (CA,cup) to be in another I mean all... (12 Replies)
Discussion started by: superprogrammer
12 Replies
Login or Register to Ask a Question